[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/tests/phpunit/testcases/ -> test_urls.php (source)

   1  <?php
   2  
   3  class GP_Test_Urls extends GP_UnitTestCase {
   4  
   5  	function setUp() {
   6          parent::setUp();
   7  
   8          $this->home_url = 'http://example.org';
   9          $this->sub_dir = '/glotpress/';
  10          $this->url = user_trailingslashit( $this->home_url . $this->sub_dir );
  11  
  12          $this->base_path_emtpy_string = '';
  13          $this->base_path_single_slash = '/';
  14  
  15          add_filter( 'gp_url_base_path', array( $this, '_gp_url_base_path_sub_dir' ) );
  16          add_filter( 'option_home', array( $this, '_gp_url_home_url' ) );
  17      }
  18  
  19  	function teardown() {
  20          parent::tearDown();
  21  
  22          remove_filter( 'gp_url_base_path', array( $this, '_gp_url_base_path_sub_dir' ) );
  23          remove_filter( 'option_home', array( $this, '_gp_url_home_url' ) );
  24      }
  25  
  26  	function _gp_url_base_path_sub_dir() {
  27          return user_trailingslashit( $this->sub_dir );
  28      }
  29  
  30  	function _gp_url_home_url() {
  31          return $this->home_url;
  32      }
  33  
  34      function test_gp_url_should_just_add_simple_path_string_if_query_is_missing() {
  35          $this->assertEquals( $this->sub_dir . 'baba', gp_url( 'baba' ) );
  36      }
  37      function test_gp_url_should_not_add_query_string_if_query_is_empty_string() {
  38          $this->assertEquals( $this->sub_dir . 'baba', gp_url( 'baba', '' ) );
  39      }
  40  
  41      function test_gp_url_should_not_add_query_string_if_query_is_empty_array() {
  42          $this->assertEquals( $this->sub_dir . 'baba', gp_url( 'baba', array() ) );
  43      }
  44  
  45      function test_gp_url_should_properly_add_query_string_if_path_is_empty() {
  46          $this->assertEquals( user_trailingslashit( $this->sub_dir ) . '?a=b', gp_url( '', '?a=b' ) );
  47      }
  48  
  49      function test_gp_url_should_add_question_mark_if_query_string_does_not_have_one() {
  50          $this->assertEquals( $this->sub_dir . 'baba?a=b', gp_url( 'baba', 'a=b' ) );
  51      }
  52  
  53  	function test_gp_url_should_expand_query_array() {
  54          $this->assertEquals( user_trailingslashit( $this->sub_dir ) . '?a=b', gp_url( '', array('a' => 'b') ) );
  55      }
  56  
  57      function test_gp_url_should_add_ampersand_if_path_is_empty_and_query_array_has_more_than_one_value() {
  58          $this->assertEquals( user_trailingslashit( $this->sub_dir ) . '?a=b&b=c', gp_url( '', array('a' => 'b', 'b' => 'c') ) );
  59      }
  60  
  61      function test_gp_url_should_add_ampersand_if_query_array_has_more_than_one_value() {
  62          $this->assertEquals( $this->sub_dir . 'baba?a=b&b=c', gp_url( 'baba', array('a' => 'b', 'b' => 'c') ) );
  63      }
  64  
  65      function test_gp_url_should_not_add_double_slash_if_path_starts_with_slash() {
  66          $this->assertEquals( $this->sub_dir . 'baba/wink', gp_url( '/baba/wink' ) );
  67      }
  68  
  69  	function test_gp_url_should_urlencode_query_var_values() {
  70          $this->assertEquals( $this->sub_dir . 'baba?a=a%26b&b=c', gp_url( 'baba', array('a' => 'a&b', 'b' => 'c') ) );
  71      }
  72  
  73      function test_gp_url_join_should_return_the_string_if_single_string_without_slashes_is_passed() {
  74          $this->assertEquals( 'baba', gp_url_join( 'baba' ) );
  75      }
  76  
  77      function test_gp_url_join_should_join_with_slash_two_strings_without_slashes() {
  78          $this->assertEquals( 'baba/dyado', gp_url_join( 'baba', 'dyado' ) );
  79      }
  80  
  81      function test_gp_url_join_should_include_only_one_slash_if_first_string_ends_with_slash_and_next_begins_with_slash() {
  82          $this->assertEquals( 'baba/dyado', gp_url_join( 'baba/', '/dyado' ) );
  83      }
  84  
  85      function test_gp_url_join_should_discard_multiple_slashes_in_the_end_of_component() {
  86          $this->assertEquals( '/baba/dyado', gp_url_join( '/baba//', 'dyado' ) );
  87      }
  88  
  89      function test_gp_url_join_should_discard_multiple_slashes_in_the_beginning_of_component() {
  90          $this->assertEquals( '/baba/dyado', gp_url_join( '/baba/', '//dyado' ) );
  91      }
  92  
  93      function test_gp_url_join_should_not_discard_slash_if_the_whole_first_component_is_slash() {
  94              $this->assertEquals( user_trailingslashit( '/baba/' ), gp_url_join( '/baba/', '/' ) );
  95              $this->assertEquals( user_trailingslashit( '/baba/' ), gp_url_join( '/baba/', '//' ) );
  96      }
  97  
  98      function test_gp_url_join_should_not_discard_slash_if_the_whole_last_component_is_slash() {
  99              $this->assertEquals( user_trailingslashit( '/baba/' ), gp_url_join( '/', '/baba/' ) );
 100              $this->assertEquals( user_trailingslashit( '/baba/' ), gp_url_join( '//', '/baba/' ) );
 101      }
 102  
 103      function test_gp_url_join_should_return_only_one_slash_if_the_only_component_is_a_slash() {
 104          $this->assertEquals( user_trailingslashit( '/' ), gp_url_join( '/' ) );
 105      }
 106  
 107      function test_gp_url_join_should_return_only_one_slash_if_all_components_are_slashes() {
 108          $this->assertEquals( user_trailingslashit( '/' ), gp_url_join( '/', '/' ) );
 109      }
 110  
 111      function test_gp_url_join_should_return_only_one_slash_if_all_components_are_multiple_slashes() {
 112          $this->assertEquals( user_trailingslashit( '/' )    , gp_url_join( '///', '///' ) );
 113      }
 114  
 115  	function test_gp_url_join_should_skip_empty_components() {
 116          $this->assertEquals( 'a/b', gp_url_join( 'a', '', 'b' ) );
 117      }
 118  
 119  	function test_gp_url_join_should_skip_empty_components_in_the_beginning() {
 120          $this->assertEquals( 'a/b', gp_url_join( '', 'a', 'b' ) );
 121      }
 122  
 123  	function test_gp_url_join_should_skip_empty_components_in_the_end() {
 124          $this->assertEquals( 'a/b', gp_url_join( 'a', 'b', '' ) );
 125      }
 126  
 127      function test_gp_url_join_should_accept_array_component_with_one_element_and_return_this_element() {
 128          $this->assertEquals( 'baba', gp_url_join( array( 'baba' ) ) );
 129      }
 130  
 131      function test_gp_url_join_should_join_array_component_values_as_if_they_were_given_as_different_arguments() {
 132          $this->assertEquals( 'baba/dyado', gp_url_join( array( 'baba', 'dyado' ) ) );
 133      }
 134  
 135  	function test_gp_url_join_should_flatten_nested_arrays() {
 136          $this->assertEquals( 'baba/dyado/chicho/lelya', gp_url_join( array( 'baba', array( 'dyado', array( 'chicho' ), 'lelya' ) ) ) );
 137      }
 138  
 139      function test_gp_url_join_should_return_empty_string_with_nested_empty_arrays() {
 140          $this->assertEquals( '', gp_url_join( array( array() ), array() ) );
 141      }
 142  
 143  	function test_gp_url_join_should_not_break_http() {
 144          $this->assertEquals( 'http://dir.bg/baba', gp_url_join( 'http://dir.bg/', 'baba' ) );
 145      }
 146  
 147  	function test_gp_url_join_should_not_break_https() {
 148          $this->assertEquals( 'https://dir.bg/baba', gp_url_join( 'https://dir.bg/', 'baba' ) );
 149      }
 150  
 151  	function test_gp_url_project_should_join_its_arguments() {
 152          $url_from_gp_url_project = gp_url_project( '/x', 'import-originals' );
 153          $url_manually_joined = gp_url_join( gp_url_project( '/x' ), 'import-originals' );
 154           $this->assertEquals( $url_manually_joined, $url_from_gp_url_project );
 155      }
 156  
 157      function test_gp_url_project_should_join_its_array_arguments() {
 158          $url_from_gp_url_project = gp_url_project( '/x', array( 'slug', 'slugslug', 'import-translations' ) );
 159          $url_manually_joined = gp_url_join( gp_url_project( '/x' ), 'slug', 'slugslug', 'import-translations' );
 160          $this->assertEquals( $url_manually_joined, $url_from_gp_url_project );
 161      }
 162  
 163  	function test_gp_url_current_should_return_http_url() {
 164          $server_vars = $_SERVER;
 165          $_SERVER['HTTPS'] = 0;
 166          $_SERVER['HTTP_HOST'] = 'glotpress.org';
 167          $_SERVER['REQUEST_URI'] = '/';
 168          $_SERVER['SERVER_PORT'] = 80;
 169          $this->assertEquals( 'http://glotpress.org/', gp_url_current() );
 170          $_SERVER = $server_vars;
 171      }
 172  
 173  	function test_gp_url_current_should_return_https_url() {
 174          $server_vars = $_SERVER;
 175          $_SERVER['HTTPS'] = 1;
 176          $_SERVER['HTTP_HOST'] = 'glotpress.org';
 177          $_SERVER['REQUEST_URI'] = '/';
 178          $_SERVER['SERVER_PORT'] = 443;
 179          $this->assertEquals( 'https://glotpress.org/', gp_url_current() );
 180          $_SERVER = $server_vars;
 181      }
 182  
 183      function test_gp_url_current_should_return_non_standard_port_url() {
 184          $server_vars = $_SERVER;
 185          $_SERVER['HTTPS'] = 0;
 186          $_SERVER['HTTP_HOST'] = 'glotpress.org:8888';
 187          $_SERVER['REQUEST_URI'] = '/';
 188          $_SERVER['SERVER_PORT'] = 8888;
 189          $this->assertEquals( 'http://glotpress.org:8888/', gp_url_current() );
 190          $_SERVER = $server_vars;
 191      }
 192  
 193      /**
 194       * @ticket gh-203
 195       */
 196  	function test_gp_url_base_path_filter() {
 197          remove_filter( 'gp_url_base_path', array( $this, '_gp_url_base_path_sub_dir' ) );
 198          add_filter( 'gp_url_base_path', array( $this, '_gp_url_base_path_filter_single_slash' ) );
 199  
 200          $this->assertSame( $this->base_path_single_slash, gp_url_base_path() );
 201          $this->assertSame( 'http://example.org' . $this->base_path_single_slash, gp_url_public_root() );
 202  
 203          remove_filter( 'gp_url_base_path', array( $this, '_gp_url_base_path_filter_single_slash' ) );
 204      }
 205  
 206  	function _gp_url_base_path_filter_single_slash() {
 207          return $this->base_path_single_slash;
 208      }
 209  
 210      /**
 211       * @ticket gh-203
 212       */
 213      function test_gp_url_returns_leading_slash_when_permalinks_have_no_trailing_slash() {
 214          remove_filter( 'gp_url_base_path', array( $this, '_gp_url_base_path_sub_dir' ) );
 215          add_filter( 'gp_url_base_path', array( $this, '_gp_url_base_path_filter_empty_string' ) );
 216          $this->set_permalink_structure( GP_TESTS_PERMALINK_STRUCTURE );
 217  
 218          $this->assertSame( '/foo/bar', gp_url( 'foo/bar' ) );
 219  
 220          remove_filter( 'gp_url_base_path', array( $this, '_gp_url_base_path_filter_empty_string' ) );
 221      }
 222  
 223  	function _gp_url_base_path_filter_empty_string() {
 224          return $this->base_path_emtpy_string;
 225      }
 226  
 227      /**
 228       * @ticket gh-203
 229       */
 230  	function test_gp_url_path_returns_single_slash() {
 231          $this->assertSame( '/', gp_url_path( 'http://glotpress.org/' ) );
 232      }
 233  
 234      /**
 235       * @ticket gh-203
 236       */
 237      function test_gp_url_path_returns_empty_string_if_url_has_no_path() {
 238          $this->assertSame( '', gp_url_path( 'http://glotpress.org' ) );
 239      }
 240  
 241      /**
 242       * @ticket gh-203
 243       */
 244      function test_gp_url_public_root_has_no_trailing_slash_when_permalinks_have_no_trailing_slash() {
 245          $this->set_permalink_structure( GP_TESTS_PERMALINK_STRUCTURE );
 246  
 247          $this->assertTrue( '/' !== substr( gp_url_public_root(), -1 ) );
 248      }
 249  
 250      /**
 251       * @ticket gh-203
 252       */
 253      function test_gp_url_public_root_has_a_trailing_slash_when_permalinks_have_a_trailing_slash() {
 254          $this->set_permalink_structure( GP_TESTS_PERMALINK_STRUCTURE_WITH_TRAILING_SLASH );
 255  
 256          $this->assertTrue( '/' === substr( gp_url_public_root(), -1 ) );
 257      }
 258  }


Generated: Sun Dec 22 01:01:04 2024 Cross-referenced by PHPXref 0.7.1