SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/tests/urlTest.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. use Sprout\Helpers\Url;
  15.  
  16.  
  17. class urlTest extends PHPUnit_Framework_TestCase
  18. {
  19.  
  20. public function testCheckRedirect()
  21. {
  22. // Correct (no querystring)
  23. $this->assertTrue(Url::checkRedirect('/hello/world'));
  24. $this->assertTrue(Url::checkRedirect('hello/world'));
  25. $this->assertTrue(Url::checkRedirect('hello/wor_ld'));
  26. $this->assertTrue(Url::checkRedirect('hello/wor-ld'));
  27. $this->assertTrue(Url::checkRedirect(''));
  28.  
  29. // Correct (with querystring)
  30. $this->assertTrue(Url::checkRedirect('/hello/world', true));
  31. $this->assertTrue(Url::checkRedirect('hello/world', true));
  32. $this->assertTrue(Url::checkRedirect('hello/wor_ld', true));
  33. $this->assertTrue(Url::checkRedirect('hello/wor-ld', true));
  34. $this->assertTrue(Url::checkRedirect('', true));
  35. $this->assertTrue(Url::checkRedirect('/hello/world?id=123', true));
  36. $this->assertTrue(Url::checkRedirect('hello/world?id=123', true));
  37. $this->assertTrue(Url::checkRedirect('hello/wor_ld?id=123', true));
  38. $this->assertTrue(Url::checkRedirect('hello/wor-ld?id=123', true));
  39. $this->assertTrue(Url::checkRedirect('hello/wor_ld?id=12-3', true));
  40. $this->assertTrue(Url::checkRedirect('hello/wor-ld?id=12_3', true));
  41. $this->assertTrue(Url::checkRedirect('hello/world?id=12_3', true));
  42. $this->assertTrue(Url::checkRedirect('hello/world?id=123%204', true));
  43. $this->assertTrue(Url::checkRedirect('hello/world?id=%20', true));
  44. $this->assertTrue(Url::checkRedirect('hello/world?id=aa_%20-bb', true));
  45.  
  46. // Incorrect (querystring not allowed)
  47. $this->assertFalse(Url::checkRedirect('/hello/world?id=123', false));
  48. $this->assertFalse(Url::checkRedirect('hello/world?id=123', false));
  49. $this->assertFalse(Url::checkRedirect('hello/wo_rld?id=123', false));
  50. $this->assertFalse(Url::checkRedirect('hello/wo-rld?id=123', false));
  51.  
  52. // Incorrect (no base url)
  53. $this->assertFalse(Url::checkRedirect('?id=123', false));
  54. $this->assertFalse(Url::checkRedirect('?id=123', true));
  55.  
  56. // Incorrect (contains protocol)
  57. $this->assertFalse(Url::checkRedirect('http://www.evil.com', false));
  58. $this->assertFalse(Url::checkRedirect('http://www.evil.com', true));
  59. $this->assertFalse(Url::checkRedirect('https://www.evil.com', false));
  60. $this->assertFalse(Url::checkRedirect('https://www.evil.com', true));
  61. $this->assertFalse(Url::checkRedirect('ftp://www.evil.com', false));
  62. $this->assertFalse(Url::checkRedirect('ftp://www.evil.com', true));
  63.  
  64. // Incorrect (assumed protocol)
  65. $this->assertFalse(Url::checkRedirect('://www.evil.com', false));
  66. $this->assertFalse(Url::checkRedirect('://www.evil.com', true));
  67.  
  68. // Incorrect (weirdness)
  69. $this->assertFalse(Url::checkRedirect("\0"));
  70. $this->assertFalse(Url::checkRedirect("\r"));
  71. $this->assertFalse(Url::checkRedirect("\t"));
  72. $this->assertFalse(Url::checkRedirect("\n"));
  73.  
  74. // Incorrect (non-strings)
  75. $this->assertFalse(Url::checkRedirect(array()));
  76. $this->assertFalse(Url::checkRedirect(new stdClass));
  77. }
  78.  
  79.  
  80. public function dataAddSocialDomain()
  81. {
  82. return [
  83. ['kbtestbot3000', 'instagram.com', 'https://instagram.com/kbtestbot3000'],
  84. ['https://instagram.com/kbtestbot3000', 'instagram.com', 'https://instagram.com/kbtestbot3000'],
  85. ['https://instagram.com/kbtestbot3000?xx', 'instagram.com', 'https://instagram.com/kbtestbot3000?xx'],
  86. ['https://instagram.com/kbtestbot3000#xx', 'instagram.com', 'https://instagram.com/kbtestbot3000#xx'],
  87. ['http://instagram.com/kbtestbot3000', 'instagram.com', 'http://instagram.com/kbtestbot3000'],
  88. ['HTTP://instagram.com/kbtestbot3000', 'instagram.com', 'http://instagram.com/kbtestbot3000'],
  89. ['HTTPS://INSTAGRAM.com/kbtestbot3000', 'instagram.com', 'https://instagram.com/kbtestbot3000'],
  90. ['instagram.com/kbtestbot3000', 'instagram.com', 'https://instagram.com/kbtestbot3000'],
  91. ['www.instagram.com/kbtestbot3000', 'instagram.com', 'https://instagram.com/kbtestbot3000'],
  92. ];
  93. }
  94.  
  95. /**
  96.   * @dataProvider dataAddSocialDomain
  97.   **/
  98. public function testAddSocialDomain($social_link, $domain, $expected)
  99. {
  100. $this->assertEquals($expected, Url::addSocialDomain($social_link, $domain));
  101. }
  102.  
  103.  
  104. public function dataAddUrlScheme()
  105. {
  106. return [
  107. ['example.com', 'http://example.com'],
  108. ['example.com/xxx/yyy?zzz', 'http://example.com/xxx/yyy?zzz'],
  109. ['example.com/http/https?http=https', 'http://example.com/http/https?http=https'],
  110. ['http://example.com', 'http://example.com'],
  111. ['https://example.com', 'https://example.com'],
  112. ['HTTP://example.com', 'HTTP://example.com'],
  113. ['HTTPS://example.com', 'HTTPS://example.com'],
  114. ];
  115. }
  116.  
  117. /**
  118.   * @dataProvider dataAddUrlScheme
  119.   **/
  120. public function testAddUrlScheme($url, $expected)
  121. {
  122. $this->assertEquals($expected, Url::addUrlScheme($url));
  123. }
  124.  
  125. }
  126.