[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

/tests/tests/format/ -> FormatTest.php (source)

   1  <?php
   2  
   3  /**
   4   * General formatting functions.
   5   *
   6   * @since 0.1
   7   */
   8  #[\PHPUnit\Framework\Attributes\Group('formatting')]
   9  class FormatTest extends PHPUnit\Framework\TestCase {
  10  
  11      /**
  12       * Data to serialize
  13       */
  14      static function serialize_data(): \Iterator
  15      {
  16          yield array( null );
  17          yield array( true );
  18          yield array( false );
  19          yield array( -25 );
  20          yield array( 25 );
  21          yield array( 1.1 );
  22          yield array( 'this string will be serialized' );
  23          yield array( "a\nb" );
  24          yield array( array() );
  25          yield array( array(1,1,2,3,5,8,13) );
  26          yield array( (object)array('test' => true, '3', 4) );
  27      }
  28  
  29      /**
  30       * Unserialized data
  31       */
  32      static function not_serialized_data(): \Iterator
  33      {
  34          yield array( 'a string' );
  35          yield array( 'garbage:a:0:garbage;' );
  36          // array( 'b:4;' ), // this test fails in WP test suite, not sure if intentional or what...
  37          yield array( 's:4:test;' );
  38      }
  39  
  40      /**
  41       * Check that yourls_is_serialized detects serialized data
  42       *
  43       * @since 0.1
  44       */
  45      #[\PHPUnit\Framework\Attributes\DataProvider('serialize_data')]
  46      public function test_is_serialized( $data ) {
  47          $this->assertTrue( yourls_is_serialized( serialize( $data ) ) );
  48      }
  49  
  50      /**
  51       * Check that yourls_is_serialized doesn't assume garbage is serialized
  52       *
  53       * @since 0.1
  54       */
  55      #[\PHPUnit\Framework\Attributes\DataProvider('not_serialized_data')]
  56      public function test_is_not_serialized( $data ) {
  57          $this->assertFalse( yourls_is_serialized( $data ) );
  58      }
  59  
  60      /**
  61       * Integer (1337) to string (3jk) to integer
  62       *
  63       * @since 0.1
  64       */
  65      public function test_int_to_string_to_int() {
  66          // 10 random integers
  67          $rnd = array();
  68          for( $i=0; $i<10; $i++ ) {
  69              $rnd[]= mt_rand( 1, 1000000 );
  70          }
  71  
  72          foreach( $rnd as $integer ) {
  73              $this->assertEquals( $integer, yourls_string2int( yourls_int2string( $integer ) ) );
  74          }
  75  
  76      }
  77  
  78      /**
  79       * String (3jk) to integer (1337) to string
  80       *
  81       * @since 0.1
  82       */
  83      public function test_string_to_int_to_string() {
  84          // 10 random strings that do not start with a zero
  85          $rnd = array();
  86          $i = 0;
  87          while( $i < 10 ) {
  88              if( $notempty = ltrim( rand_str( mt_rand( 2, 10 ) ), '0' ) ) {
  89                  $rnd[]= $notempty;
  90                  $i++;
  91              }
  92          }
  93  
  94          foreach( $rnd as $string ) {
  95              $this->assertEquals( $string, yourls_int2string( yourls_string2int( $string ) ) );
  96          }
  97      }
  98  
  99      /**
 100       * Checking that yourls_unique_element_id is a unique string
 101       *
 102       */
 103      public function test_string2htmlid() {
 104          $id1 = yourls_unique_element_id();
 105          $id2 = yourls_unique_element_id();
 106          $id3 = yourls_unique_element_id('foo', 10);
 107          $id4 = yourls_unique_element_id();
 108          $this->assertIsString($id1);
 109          $this->assertIsString($id2);
 110          $this->assertNotSame($id1, $id2);
 111          $this->assertEquals('foo10', $id3, 'ID is built using the specified prefix and counter value.');
 112          $this->assertStringEndsWith('11', $id4, 'ID counter continues to increment from the last value.');
 113      }
 114  
 115      /**
 116       * Generating valid regexp from the allowed charset
 117       *
 118       * @since 0.1
 119       */
 120      function test_valid_regexp() {
 121          $pattern = yourls_make_regexp_pattern( yourls_get_shorturl_charset() );
 122  
 123          /* To validate a RegExp just run it against an empty string.
 124             If it returns explicit false (=== false), it's broken. Otherwise it's valid.
 125             From: https://stackoverflow.com/a/12941133/36850
 126             Cool to know :)
 127  
 128             We're testing it as used in yourls_sanitize_keyword()
 129             TODO: more random char strings to test?
 130          */
 131  
 132          $this->assertNotFalse( preg_match( '![^' . $pattern . ']!', '' ) );
 133      }
 134  
 135      /**
 136       * Trim long strings
 137       *
 138       * @since 0.1
 139       */
 140      function test_trim_long_strings() {
 141          $long = "The Plague That Makes Your Booty Move... It's The Infectious Grooves";
 142          $trim = "The Plague That Makes Your Booty Move... It's The Infec[...]";
 143          $this->assertSame( $trim, yourls_trim_long_string( $long ) );
 144  
 145          $long = "The Plague That Makes Your Booty Move... It's The Infectious Grooves";
 146          $trim = "The Plague That Makes Your Booty[...]";
 147          $this->assertSame( $trim, yourls_trim_long_string( $long, 37 ) );
 148  
 149          $long = "The Plague That Makes Your Booty Move... It's The Infectious Grooves";
 150          $trim = "The Plague That Makes Your Booty Mo..";
 151          $this->assertSame( $trim, yourls_trim_long_string( $long, 37, '..' ) );
 152      }
 153  
 154      /**
 155       * Return true for UTF8 strings
 156       *
 157       * Note: As of 1.7.1, function yourls_seem_utf8() is still unused. In 2.0 consider simply deleting it if still not needed
 158       *
 159       * @since 0.1
 160       */
 161      #[\PHPUnit\Framework\Attributes\DataProvider('valid_utf8')]
 162      function test_is_utf8( $string ) {
 163          $this->assertTrue( yourls_seems_utf8( $string ) );
 164      }
 165  
 166      /**
 167       * Return false for non UTF8 strings
 168       *
 169       * Note: As of 1.7.1, function yourls_seem_utf8() is still unused. In 2.0 consider simply deleting it if still not needed
 170       *
 171       * @since 0.1
 172       */
 173      #[\PHPUnit\Framework\Attributes\DataProvider('invalid_utf8')]
 174      function test_is_not_utf8( $string ) {
 175          $this->assertFalse( yourls_seems_utf8( $string ) );
 176      }
 177  
 178      static function valid_utf8() {
 179          return self::get_data( YOURLS_TESTDATA_DIR . '/formatting/utf-8.txt' );
 180      }
 181  
 182      static function invalid_utf8() {
 183          return self::get_data( YOURLS_TESTDATA_DIR . '/formatting/big5.txt' );
 184      }
 185  
 186      /**
 187       * Parse a file and return its content as a data provider
 188       */
 189      static function get_data( $filename ) {
 190          $strings = file( $filename );
 191          foreach ( $strings as &$string ) {
 192              $string = (array) trim( $string );
 193          }
 194          unset( $string );
 195          return $strings;
 196      }
 197  
 198      /**
 199       * Test yourls_backslashit
 200       *
 201       * @since 0.1
 202       */
 203      function test_backslashit() {
 204          $this->assertSame( '\h\e\l\l\o \w\o\r\l\d 123 !', yourls_backslashit( 'hello world 123 !' ) );
 205          $this->assertSame( '\\\1, 2, 3', yourls_backslashit( '1, 2, 3' ) );
 206      }
 207  
 208      /**
 209       * Test the bookmarklet generator
 210       *
 211       * Note: we're not testing that the bookmarklet generator produces valid JS code: the
 212       * bookmarklet class has tests for this, see https://github.com/ozh/bookmarkletgen
 213       * We're just testing that content is returned
 214       *
 215       * @since 0.1
 216       */
 217      function test_bookmarklet() {
 218          $code = yourls_make_bookmarklet( 'hello' );
 219          $this->assertTrue( is_string( $code ) );
 220      }
 221  
 222      /**
 223       * Test yourls_specialchars basics
 224       *
 225       * @since 0.1
 226       */
 227      function test_specialchars_decode_basics() {
 228          $html =  "&amp;&lt;hello world&gt;";
 229          $this->assertEquals( $html, yourls_specialchars( $html ) );
 230  
 231          $double = "&amp;amp;&amp;lt;hello world&amp;gt;";
 232          $this->assertEquals( $double, yourls_specialchars( $html, ENT_NOQUOTES, true ) );
 233      }
 234  
 235      /**
 236       * Test yourls_specialchars escape quotes
 237       *
 238       * @since 0.1
 239       */
 240      function test_specialchars_escapes_quotes() {
 241          $source = "\"'hello!'\"";
 242          $this->assertEquals( '"&#039;hello!&#039;"', yourls_specialchars( $source, 'single' ) );
 243          $this->assertEquals( "&quot;'hello!'&quot;", yourls_specialchars( $source, 'double' ) );
 244          $this->assertEquals( '&quot;&#039;hello!&#039;&quot;', yourls_specialchars( $source, ENT_QUOTES, true ) );
 245          $this->assertEquals( '&quot;&#039;hello!&#039;&quot;', yourls_specialchars( $source, 'omg', true ) ); // unaccepted value should be treated as ENT_QUOTES
 246          $this->assertEquals( $source, yourls_specialchars( $source ) );
 247      }
 248  
 249      /**
 250       * Test yourls_specialchars doesn't change allowed entities
 251       *
 252       * @since 0.1
 253       */
 254      function test_specialchars_allowed_entities() {
 255          foreach ( yourls_kses_allowed_entities() as $ent ) {
 256              $ent = '&' . $ent . ';';
 257              $this->assertEquals( $ent, yourls_specialchars( $ent ) );
 258          }
 259      }
 260  
 261      /**
 262       * Test yourls_specialchars with unallowed entities
 263       *
 264       * @since 0.1
 265       */
 266      function test_specialchars_unallowed_entities() {
 267          $ents = array( 'iacut', 'aposs', 'pos', 'apo', 'apo?', 'apo.*', '.*apo.*', 'apos ', ' apos', ' apos ' );
 268  
 269          foreach ( $ents as $ent ) {
 270              $escaped = '&amp;' . $ent . ';';
 271              $ent = '&' . $ent . ';';
 272              $this->assertEquals( $escaped, yourls_specialchars( $ent ) );
 273          }
 274      }
 275  
 276  }


Generated: Mon Mar 31 05:10:02 2025 Cross-referenced by PHPXref 0.7.1