[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

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

   1  <?php
   2  
   3  class GP_Test_Builtin_Translation_Warnings extends GP_UnitTestCase {
   4  
   5  	function setUp() {
   6          parent::setUp();
   7          $this->w              = new GP_Builtin_Translation_Warnings();
   8          $this->l              = $this->factory->locale->create();
   9          $this->longer_than_20 = 'The little boy hid behind the counter and then came the wizard of all green wizards!';
  10          $this->shorter_than_5 = 'Boom';
  11      }
  12  
  13  	function _assertWarning( $assert, $warning, $original, $translation, $locale = null ) {
  14          if ( is_null( $locale ) ) {
  15              $locale = $this->l;
  16          }
  17          $method = "warning_$warning";
  18          $this->$assert( true, $this->w->$method( $original, $translation, $locale ) );
  19      }
  20  
  21  	function assertHasWarnings( $warning, $original, $translation, $locale = null ) {
  22          $this->_assertWarning( 'assertNotSame', $warning, $original, $translation, $locale );
  23      }
  24  
  25  	function assertNoWarnings( $warning, $original, $translation, $locale = null ) {
  26          $this->_assertWarning( 'assertSame', $warning, $original, $translation, $locale );
  27      }
  28  
  29  	function assertHasWarningsAndContainsOutput( $warning, $original, $translation, $output_expected, $locale = null ) {
  30          $this->assertHasWarnings( $warning, $original, $translation, $locale );
  31          if ( is_null( $locale ) ) {
  32              $locale = $this->l;
  33          }
  34          $method = "warning_$warning";
  35          $this->assertStringContainsString( $output_expected, $this->w->$method( $original, $translation, $locale ) );
  36      }
  37  
  38  	function assertContainsOutput( $singular, $plural, $translations, $output_expected, $locale = null ) {
  39          if ( is_null( $locale ) ) {
  40              $locale = $this->l;
  41          }
  42          $this->assertEquals( $output_expected, $this->tw->check( $singular, $plural, $translations, $locale ) );
  43      }
  44  
  45  	function test_length() {
  46          $this->assertNoWarnings( 'length', $this->longer_than_20, $this->longer_than_20 );
  47          $this->assertNoWarnings( 'length', 'number_format_', '' );
  48          $this->assertHasWarningsAndContainsOutput(
  49              'length',
  50              $this->longer_than_20,
  51              $this->shorter_than_5,
  52              'Lengths of source and translation differ too much.'
  53          );
  54      }
  55  
  56  	function test_length_exclude() {
  57          $w_without_locale                           = new GP_Builtin_Translation_Warnings();
  58          $w_without_locale->length_exclude_languages = array( $this->l->slug );
  59          $this->assertSame( true, $w_without_locale->warning_length( $this->longer_than_20, $this->longer_than_20, $this->l ) );
  60          $this->assertSame( true, $w_without_locale->warning_length( $this->longer_than_20, $this->shorter_than_5, $this->l ) );
  61      }
  62  
  63  	function test_tags() {
  64          $this->assertNoWarnings( 'tags', 'Baba', 'Баба' );
  65          $this->assertNoWarnings(
  66              'tags',
  67              '<p><abbr title="World Health Organization">WHO</abbr> was founded in 1948.</p>',
  68              '<p>La<abbr title="Organización Mundial de la Salud">OMS</abbr> se fundó en 1948.</p>'
  69          );
  70          $this->assertNoWarnings(
  71              'tags',
  72              '<button aria-label="Close">X</button><button aria-label="Open">O</button>',
  73              '<button aria-label="Cerrar">X</button><button aria-label="Abrir">A</button>'
  74          );
  75          $this->assertNoWarnings( 'tags', '<a href="%s">Baba</a>', '<a href="%s">Баба</a>' );
  76          $this->assertNoWarnings( 'tags', '<a href="%s" title="Blimp!">Baba</a>', '<a href="%s" title="Блимп!">Баба</a>' );
  77          $this->assertNoWarnings( 'tags', '<a href="%s" aria-label="Blimp!">Baba</a>', '<a href="%s" aria-label="Блимп!">Баба</a>' );
  78          $this->assertNoWarnings( 'tags', '<a href="%s" title="Blimp!" aria-label="Blimp!">Baba</a>', '<a href="%s" title="Блимп!" aria-label="Блимп!">Баба</a>' );
  79          $this->assertNoWarnings(
  80              'tags',
  81              '<a href="https://www.example.org" title="Example!" lang="en">Example URL</a>',
  82              '<a href="https://www.example.org" title="¡Ejemplo!" lang="es">URL de jemplo</a>'
  83          );
  84          $this->l->slug = 'ja';
  85          $this->assertNoWarnings(
  86              'tags',
  87              '<b>Text 1</b>, <i>Italic text</i>, Text 2, <em>Emphasized text</em>, Text 3',
  88              '<b>テキスト1</b>、イタリック体、テキスト2、エンファシス体、テキスト3',
  89              $this->l
  90          );
  91          $this->assertNoWarnings( 'tags', '</a>Incorrect link</a>', '<a>Incorrect link</a>' );
  92          $this->assertNoWarnings(
  93              'tags',
  94              ' Text 1 <a href="https://wordpress.org/plugins/example-plugin/">Example plugin</a> Text 2<a href="https://wordpress.com/log-in/">Log in</a> Text 3 <img src="example.jpg" alt="Example alt text">',
  95              ' Texto 1 <a href="https://es.wordpress.org/plugins/example-plugin/">Plugin de ejemplo</a> Texto 2<a href="https://es.wordpress.com/log-in/">Acceder</a> Texto 3 <img src="example.jpg" alt="Texto alternativo de ejemplo">'
  96          );
  97          $this->assertNoWarnings(
  98              'tags',
  99              '<img src="https://en.wikipedia.org/wiki/WordPress#/media/File:WordPress_logo.svg" alt="WordPress in the Wikipedia">',
 100              '<img src="https://es.wikipedia.org/wiki/WordPress#/media/File:WordPress_logo.svg" alt="WordPress en la Wikipedia">'
 101          );
 102          $this->assertNoWarnings(
 103              'tags',
 104              ' Text 1 <a href="https://wordpress.com/log-in">Log in</a> Text 2 <img src="https://en.gravatar.com/matt" alt="Matt\'s Gravatar"> ',
 105              ' Texto 1 <a href="https://es.wordpress.com/log-in">Acceder</a> Texto 2 <img src="https://es.gravatar.com/matt" alt="Gravatar de Matt"> '
 106          );
 107          $this->assertHasWarningsAndContainsOutput(
 108              'tags',
 109              '<p>Paragraph</p>',
 110              '<p>Párrafo',
 111              'Missing tags from translation. Expected: </p>'
 112          );
 113          $this->assertHasWarningsAndContainsOutput(
 114              'tags',
 115              'Paragraph</p>',
 116              '<p>Párrafo</p>',
 117              'Too many tags in translation. Found: <p>'
 118          );
 119          $this->assertHasWarningsAndContainsOutput(
 120              'tags',
 121              '<h1>Title</h1><p>Text 1</p><br><b>Text 2</b>',
 122              '<h1>Título</h1><p>Texto 1<br><b>Texto 2</b>',
 123              'Missing tags from translation. Expected: </p>'
 124          );
 125          $this->assertHasWarningsAndContainsOutput(
 126              'tags',
 127              '<h1>Title</h1>Text 1</p><br><b>Text 2</b>',
 128              '<h1>Título</h1><p>Texto 1</p><br><b>Texto 2</b>',
 129              'Too many tags in translation. Found: <p>'
 130          );
 131          $this->assertHasWarningsAndContainsOutput(
 132              'tags',
 133              '<a href="%s" title="Blimp!">Baba</a>',
 134              '<a href="javascript:%s" title="Блимп!">Баба</a>',
 135              'The translation contains the following unexpected links: javascript:%s'
 136          );
 137          $this->assertHasWarningsAndContainsOutput(
 138              'tags',
 139              '<a href="javascript:%s" title="Blimp!">Baba</a>',
 140              '<a href="%s" title="Блимп!">Баба</a>',
 141              'The translation appears to be missing the following links: javascript:%s'
 142          );
 143          $this->assertHasWarningsAndContainsOutput(
 144              'tags',
 145              '<a href="https://www.example.org" title="Example!">Example URL</a>',
 146              '<a href="https://www.example.com" title="¡Ejemplo!">Ejemplo</a>',
 147              "The translation appears to be missing the following URLs: https://www.example.org\nThe translation contains the following unexpected URLs: https://www.example.com"
 148          );
 149          $this->assertHasWarningsAndContainsOutput(
 150              'tags',
 151              '<a href="%s" title="Blimp!">Baba</a>',
 152              '<a href="%s" x>Баба</a>',
 153              'Expected <a href="%s" title="Blimp!">, got <a href="%s" x>.'
 154          );
 155          $this->assertHasWarningsAndContainsOutput(
 156              'tags',
 157              '<p>Baba</p>',
 158              '</p>Баба<p>',
 159              'The translation contains incorrect HTML tags: Unexpected end tag : p'
 160          );
 161          $this->assertHasWarningsAndContainsOutput(
 162              'tags',
 163              '<h1>Hello</h1><h2>Peter</h2>',
 164              '<h1>Hola</h1></h2>Pedro<h2>',
 165              'The translation contains incorrect HTML tags: Unexpected end tag : h2'
 166          );
 167          $this->assertHasWarningsAndContainsOutput(
 168              'tags',
 169              '<img src="https://es.wikipedia.org/wiki/WordPress#/media/File:WordPress_logo.svg" alt="WordPress en la Wikipedia">',
 170              '<img src="https://en.wikipedia.org/wiki/WordPress#/media/File:WordPress_logo.svg" alt="WordPress in the Wikipedia">',
 171              "The translation appears to be missing the following URLs: https://es.wikipedia.org/wiki/WordPress#/media/File:WordPress_logo.svg\nThe translation contains the following unexpected URLs: https://en.wikipedia.org/wiki/WordPress#/media/File:WordPress_logo.svg"
 172          );
 173          $this->l->slug = 'ja';
 174          $this->assertHasWarningsAndContainsOutput(
 175              'tags',
 176              '<b>Text 1</b>, <i>Italic text</i>, Text 2, <em>Emphasized text</em>, Text 3',
 177              '</b>テキスト1<b>、イタリック体、テキスト2、エンファシス体、テキスト3',
 178              'The translation contains incorrect HTML tags: Unexpected end tag : b',
 179              $this->l
 180          );
 181      }
 182  
 183  	function test_add_all() {
 184          $warnings = $this->getMockBuilder( 'GP_Translation_Warnings' )->getMock();
 185          // we check for the number of warnings, because PHPUnit doesn't allow
 186          // us to check if each argument is a callable
 187          $warnings->expects( $this->exactly( 10 ) )->method( 'add' )->will( $this->returnValue( true ) );
 188          $this->w->add_all( $warnings );
 189      }
 190  
 191  	function test_placeholders() {
 192          $this->assertNoWarnings( 'placeholders', '%s baba', '%s баба' );
 193          $this->assertNoWarnings( 'placeholders', '%s baba', 'баба %s' );
 194          $this->assertNoWarnings( 'placeholders', '%s baba', 'баба %s' );
 195          $this->assertNoWarnings( 'placeholders', '%1$s baba %2$s dyado', '%1$sбабадядо%2$s' );
 196          $this->assertNoWarnings( 'placeholders', '% baba', 'баба' );
 197          $this->assertNoWarnings( 'placeholders', '% baba', '% баба' );
 198          $this->assertNoWarnings( 'placeholders', '%1$s baba', '%1$s баба' );
 199          $this->assertNoWarnings( 'placeholders', '%sHome%s', '%sНачало%s' );
 200          $this->assertNoWarnings( 'placeholders', 'This string has %stwo variables%s.', 'Deze string heeft %stwee variabelen%s.' );
 201          $this->assertNoWarnings( 'placeholders', '%% baba', '%% баба' );
 202          $this->assertNoWarnings( 'placeholders', '%s%% baba', '%s%% баба' );
 203  
 204          $this->assertHasWarningsAndContainsOutput(
 205              'placeholders',
 206              '%s baba',
 207              'баба',
 208              'Missing %s placeholder in translation.'
 209          );
 210          $this->assertHasWarningsAndContainsOutput(
 211              'placeholders',
 212              '%s baba',
 213              '% баба',
 214              'Missing %s placeholder in translation.'
 215          );
 216          $this->assertHasWarningsAndContainsOutput(
 217              'placeholders',
 218              '%1$s baba',
 219              'баба',
 220              'Missing %1$s placeholder in translation.'
 221          );
 222          $this->assertHasWarningsAndContainsOutput(
 223              'placeholders',
 224              '%% baba',
 225              '% баба',
 226              'Missing %% placeholder in translation.'
 227          );
 228          $this->assertHasWarningsAndContainsOutput(
 229              'placeholders',
 230              '%s baba',
 231              '%%s баба',
 232              'Missing %s placeholder in translation.'
 233          );
 234          $this->assertHasWarningsAndContainsOutput(
 235              'placeholders',
 236              '%1$s baba',
 237              '%%1$s баба',
 238              'Missing %1$s placeholder in translation.'
 239          );
 240          $this->assertHasWarningsAndContainsOutput(
 241              'placeholders',
 242              'баба',
 243              '%s baba',
 244              'Extra %s placeholder in translation.'
 245          );
 246      }
 247  
 248  	function test_should_begin_end_on_newline() {
 249          $this->assertHasWarningsAndContainsOutput(
 250              'should_begin_on_newline',
 251              "\nbaba",
 252              'baba',
 253              'Original and translation should both begin on newline.'
 254          );
 255          $this->assertHasWarningsAndContainsOutput(
 256              'should_not_begin_on_newline',
 257              'baba',
 258              "\nbaba",
 259              'Translation should not begin on newline.'
 260          );
 261          $this->assertHasWarningsAndContainsOutput(
 262              'should_end_on_newline',
 263              "baba\n",
 264              'baba',
 265              'Original and translation should both end on newline.'
 266          );
 267          $this->assertHasWarningsAndContainsOutput(
 268              'should_not_end_on_newline',
 269              'baba',
 270              "baba\n",
 271              'Translation should not end on newline.'
 272          );
 273  
 274          $this->assertNoWarnings( 'should_begin_on_newline', 'baba', 'baba' );
 275          $this->assertNoWarnings( 'should_not_begin_on_newline', 'baba', 'baba' );
 276          $this->assertNoWarnings( 'should_end_on_newline', 'baba', 'baba' );
 277          $this->assertNoWarnings( 'should_not_end_on_newline', 'baba', 'baba' );
 278  
 279          $this->assertNoWarnings( 'should_begin_on_newline', "baba\n", "baba\n" );
 280          $this->assertNoWarnings( 'should_not_begin_on_newline', "baba\n", "baba\n" );
 281          $this->assertNoWarnings( 'should_end_on_newline', "baba\n", "baba\n" );
 282          $this->assertNoWarnings( 'should_not_end_on_newline', "baba\n", "baba\n" );
 283  
 284          $this->assertNoWarnings( 'should_begin_on_newline', "\nbaba", "\nbaba" );
 285          $this->assertNoWarnings( 'should_not_begin_on_newline', "\nbaba", "\nbaba" );
 286          $this->assertNoWarnings( 'should_end_on_newline', "\nbaba", "\nbaba" );
 287          $this->assertNoWarnings( 'should_not_end_on_newline', "\nbaba", "\nbaba" );
 288      }
 289  
 290  	function test_placeholders_using_check() {
 291          $w       = new GP_Translation_Warnings();
 292          $builtin = new GP_Builtin_Translation_Warnings();
 293          $w->add( 'placeholder', array( $builtin, 'warning_placeholders' ) );
 294  
 295          $fr = new GP_Locale(
 296              array(
 297                  'nplurals'          => 2,
 298                  'plural_expression' => 'n > 1',
 299              )
 300          );
 301          $this->assertEquals(
 302              null,
 303              $w->check( 'original %1$s', 'original %2$s', array( 'translation %1$s', 'translation %2$s' ), $fr )
 304          );
 305          $this->assertEquals(
 306              null,
 307              $w->check( 'original %1$s', 'original %2$s', array( null ), $fr )
 308          );
 309          $this->assertEquals(
 310              null,
 311              $w->check( 'original', 'original %s', array( 'translation', 'translation %s' ), $fr )
 312          );
 313          $this->assertEquals(
 314              array( 1 => array( 'placeholder' => 'Missing %2$s placeholder in translation.' ) ),
 315              $w->check( 'original %1$s', 'original %2$s', array( 'translation %1$s', 'translation' ), $fr )
 316          );
 317          $this->assertEquals(
 318              array( 0 => array( 'placeholder' => 'Missing %1$s placeholder in translation.' ) ),
 319              $w->check( 'original %1$s', 'original %2$s', array( 'translation', 'translation  %2$s' ), $fr )
 320          );
 321  
 322          $de = new GP_Locale(
 323              array(
 324                  'nplurals'          => 2,
 325                  'plural_expression' => 'n != 1',
 326              )
 327          );
 328          $this->assertEquals(
 329              null,
 330              $w->check( 'original %1$s', 'original %2$s', array( 'translation %1$s', 'translation %2$s' ), $de )
 331          );
 332          $this->assertEquals(
 333              null,
 334              $w->check( 'original', 'original %s', array( 'translation', 'translation %s' ), $de )
 335          );
 336  
 337          $ja = new GP_Locale(
 338              array(
 339                  'nplurals'          => 1,
 340                  'plural_expression' => '0',
 341              )
 342          );
 343  
 344          $this->assertEquals(
 345              null,
 346              $w->check( 'original %1$s', 'original %2$s', array( 'translation %2$s' ), $ja )
 347          );
 348          $this->assertEquals(
 349              null,
 350              $w->check( 'original', 'original %s', array( 'translation %s' ), $ja )
 351          );
 352          $this->assertEquals(
 353              array( 0 => array( 'placeholder' => 'Missing %2$s placeholder in translation.' ) ),
 354              $w->check( 'original %1$s', 'original %2$s', array( 'translation' ), $ja )
 355          );
 356  
 357          $ru = new GP_Locale(
 358              array(
 359                  'nplurals'          => 3,
 360                  'plural_expression' => '(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)',
 361              )
 362          );
 363  
 364          $this->assertEquals(
 365              null,
 366              $w->check( 'original %1$s', 'original %2$s', array( 'translation %1$s', 'translation %2$s', 'translation 2 %2$s' ), $ru )
 367          );
 368          $this->assertEquals(
 369              null,
 370              $w->check( 'original', 'original %s', array( 'translation', 'translation 2 %s', 'translation 3 %s' ), $ru )
 371          );
 372          $this->assertEquals(
 373              array( 1 => array( 'placeholder' => 'Missing %2$s placeholder in translation.' ) ),
 374              $w->check( 'original %1$s', 'original %2$s', array( 'translation %1$s', 'translation 2', 'translation 3 %2$s' ), $ru )
 375          );
 376          $this->assertEquals(
 377              array( 2 => array( 'placeholder' => 'Missing %s placeholder in translation.' ) ),
 378              $w->check( 'original', 'original %s', array( 'translation', 'translation 2 %s', 'translation 3' ), $ru )
 379          );
 380          $this->assertEquals(
 381              array(
 382                  1 => array( 'placeholder' => 'Missing %s placeholder in translation.' ),
 383                  2 => array( 'placeholder' => 'Missing %s placeholder in translation.' ),
 384              ),
 385              $w->check( 'original', 'original %s', array( 'translation', 'translation 2', 'translation 3' ), $ru )
 386          );
 387      }
 388  
 389  	function test_mismatching_urls() {
 390          $this->assertNoWarnings( 'mismatching_urls', 'https://www.example', 'https://www.example' );
 391          $this->assertNoWarnings( 'mismatching_urls', 'http://www.example', 'http://www.example' );
 392          $this->assertNoWarnings( 'mismatching_urls', '//www.example', '//www.example' );
 393          $this->assertNoWarnings( 'mismatching_urls', '"//www.example"', '"//www.example.com"' );
 394          $this->assertNoWarnings( 'mismatching_urls', "'//www.example'", "'//www.example.com'" );
 395          $this->assertNoWarnings( 'mismatching_urls', '// www.example', '// www.example.comte    ' );
 396          $this->assertNoWarnings( 'mismatching_urls', 'http://127.0.0.1', 'https://127.0.0.1' );
 397          $this->assertNoWarnings( 'mismatching_urls', 'https://127.0.0.1', 'http://127.0.0.1' );
 398          $this->assertNoWarnings( 'mismatching_urls', 'https://www.example.com', 'https://www.example.com/' );
 399          $this->assertNoWarnings( 'mismatching_urls', 'https://www.example.com/', 'https://www.example.com' );
 400          $this->assertNoWarnings( 'mismatching_urls', 'http://www.example.com', 'https://www.example.com/' );
 401          $this->assertNoWarnings( 'mismatching_urls', 'http://www.example.com/', 'https://www.example.com' );
 402          $this->assertNoWarnings( 'mismatching_urls', 'http://wordpress.org/plugins/example-plugin/', 'https://wordpress.org/plugins/example-plugin' );
 403          $this->assertNoWarnings( 'mismatching_urls', 'https://wordpress.org/plugins/example-plugin', 'http://wordpress.org/plugins/example-plugin/' );
 404          $this->assertNoWarnings( 'mismatching_urls', 'http://www.example.com/wp-content/uploads/2020/12/logo.png', 'https://www.example.com/wp-content/uploads/2020/12/logo.png' );
 405          $this->assertNoWarnings( 'mismatching_urls', 'Text1 https://www.example.com Text2 https://www.example.org Text3', 'Texto1 https://www.example.com Texto2 https://www.example.org Texto3' );
 406          $this->assertNoWarnings( 'mismatching_urls', 'Text1 https://www.example.com Text2 https://www.example.org Text3', ' Texto3 https://www.example.org Texto2 https://www.example.com Texto1  ' );
 407          $this->assertNoWarnings( 'mismatching_urls', 'Text1 https://www.example.com Text2 https://www.example.org Text3', '  https://www.example.org Texto1   Texto3   https://www.example.com  Texto2  ' );
 408          $this->assertNoWarnings( 'mismatching_urls', 'Text1 https://www.example.com Text2 https://www.example.org Text3', '  https://www.example.org https://www.example.com ' );
 409          $this->assertNoWarnings( 'mismatching_urls', 'https://wordpress.org/plugins/example-plugin/', 'https://es.wordpress.org/plugins/example-plugin/' );
 410          $this->assertNoWarnings( 'mismatching_urls', 'https://wordpress.com/log-in/', 'https://es.wordpress.com/log-in/' );
 411          $this->assertNoWarnings( 'mismatching_urls', 'https://en.gravatar.com/matt', 'https://es.gravatar.com/matt' );
 412          $this->assertNoWarnings( 'mismatching_urls', 'https://en.wikipedia.org/wiki/WordPress', 'https://es.wikipedia.org/wiki/WordPress' );
 413  
 414          $this->assertHasWarningsAndContainsOutput(
 415              'mismatching_urls',
 416              'HTTPS://WWW.EXAMPLE',
 417              'https://www.example',
 418              "The translation appears to be missing the following URLs: HTTPS://WWW.EXAMPLE\nThe translation contains the following unexpected URLs: https://www.example"
 419          );
 420          $this->assertHasWarningsAndContainsOutput(
 421              'mismatching_urls',
 422              'https://www.example',
 423              'HTTPS://WWW.EXAMPLE',
 424              "The translation appears to be missing the following URLs: https://www.example\nThe translation contains the following unexpected URLs: HTTPS://WWW.EXAMPLE"
 425          );
 426          $this->assertHasWarningsAndContainsOutput(
 427              'mismatching_urls',
 428              'HtTpS://WwW.eXaMpLe',
 429              'https://www.example',
 430              "The translation appears to be missing the following URLs: HtTpS://WwW.eXaMpLe\nThe translation contains the following unexpected URLs: https://www.example"
 431          );
 432          $this->assertHasWarningsAndContainsOutput(
 433              'mismatching_urls',
 434              'https://www.example.com',
 435              'https://www.example.org',
 436              "The translation appears to be missing the following URLs: https://www.example.com\nThe translation contains the following unexpected URLs: https://www.example.org"
 437          );
 438          $this->assertHasWarningsAndContainsOutput(
 439              'mismatching_urls',
 440              '//www.example.com',
 441              'http://www.example.org',
 442              "The translation appears to be missing the following URLs: //www.example.com\nThe translation contains the following unexpected URLs: http://www.example.org"
 443          );
 444          $this->assertHasWarningsAndContainsOutput(
 445              'mismatching_urls',
 446              '//www.example.com',
 447              'https://www.example.org',
 448              "The translation appears to be missing the following URLs: //www.example.com\nThe translation contains the following unexpected URLs: https://www.example.org"
 449          );
 450          $this->assertHasWarningsAndContainsOutput(
 451              'mismatching_urls',
 452              'http://www.example.com',
 453              '//www.example.org',
 454              "The translation appears to be missing the following URLs: http://www.example.com\nThe translation contains the following unexpected URLs: //www.example.org"
 455          );
 456          $this->assertHasWarningsAndContainsOutput(
 457              'mismatching_urls',
 458              'https://www.example.com',
 459              '//www.example.org',
 460              "The translation appears to be missing the following URLs: https://www.example.com\nThe translation contains the following unexpected URLs: //www.example.org"
 461          );
 462          $this->assertHasWarningsAndContainsOutput(
 463              'mismatching_urls',
 464              'https://www.exañple.com',
 465              'https://www.example.com',
 466              "The translation appears to be missing the following URLs: https://www.exañple.com\nThe translation contains the following unexpected URLs: https://www.example.com"
 467          );
 468          $this->assertHasWarningsAndContainsOutput(
 469              'mismatching_urls',
 470              'https://www.example.com',
 471              'https://www.exañple.com',
 472              "The translation appears to be missing the following URLs: https://www.example.com\nThe translation contains the following unexpected URLs: https://www.exañple.com"
 473          );
 474          $this->assertHasWarningsAndContainsOutput(
 475              'mismatching_urls',
 476              'https://www.wordpress.org/plugins/example-plugin/',
 477              'https://es.wordpress.org/plugins/example-plugin/',
 478              "The translation appears to be missing the following URLs: https://www.wordpress.org/plugins/example-plugin/\nThe translation contains the following unexpected URLs: https://es.wordpress.org/plugins/example-plugin/"
 479          );
 480          $this->assertHasWarningsAndContainsOutput(
 481              'mismatching_urls',
 482              'https://www.wordpress.com/log-in/',
 483              'https://es.wordpress.com/log-in/',
 484              "The translation appears to be missing the following URLs: https://www.wordpress.com/log-in/\nThe translation contains the following unexpected URLs: https://es.wordpress.com/log-in/"
 485          );
 486          $this->assertHasWarningsAndContainsOutput(
 487              'mismatching_urls',
 488              'https://es.gravatar.com/matt',
 489              'https://en.gravatar.com/matt',
 490              "The translation appears to be missing the following URLs: https://es.gravatar.com/matt\nThe translation contains the following unexpected URLs: https://en.gravatar.com/matt"
 491          );
 492          $this->assertHasWarningsAndContainsOutput(
 493              'mismatching_urls',
 494              'https://es.wikipedia.org/wiki/WordPress',
 495              'https://en.wikipedia.org/wiki/WordPress',
 496              "The translation appears to be missing the following URLs: https://es.wikipedia.org/wiki/WordPress\nThe translation contains the following unexpected URLs: https://en.wikipedia.org/wiki/WordPress"
 497          );
 498          $this->assertHasWarningsAndContainsOutput(
 499              'mismatching_urls',
 500              'Text1 https://www.example.com Text2',
 501              'Texto1 Texto2',
 502              'The translation appears to be missing the following URLs: https://www.example.com'
 503          );
 504          $this->assertHasWarningsAndContainsOutput(
 505              'mismatching_urls',
 506              'Text1 Text2',
 507              'Texto1 https://www.example.com Texto2',
 508              'The translation contains the following unexpected URLs: https://www.example.com'
 509          );
 510          $this->assertHasWarningsAndContainsOutput(
 511              'mismatching_urls',
 512              'Text1 https://www.example.com Text2 https://www.example.org',
 513              'Texto1 https://www.example.com Texto2',
 514              'The translation appears to be missing the following URLs: https://www.example.org'
 515          );
 516          $this->assertHasWarningsAndContainsOutput(
 517              'mismatching_urls',
 518              'Text1 https://www.example.com Text2',
 519              'Texto1 https://www.example.com Texto2 https://www.example.org',
 520              'The translation contains the following unexpected URLs: https://www.example.org'
 521          );
 522      }
 523  
 524  
 525  	function test_unexpected_sprintf_token() {
 526          $this->assertNoWarnings( 'unexpected_sprintf_token', '100 percent', '100%' );
 527          $this->assertNoWarnings( 'unexpected_sprintf_token', '<a href="%a">100 percent</a>', '<a href="%a">100%</a>' );
 528          $this->assertNoWarnings( 'unexpected_sprintf_token', '<a href="%s">100 percent</a>', '<a href="%s">100%%</a>' );
 529          $this->assertNoWarnings( 'unexpected_sprintf_token', '<a href="%1$s">100 percent</a>', '<a href="%1$s">100%%</a>' );
 530          $this->assertNoWarnings(
 531              'unexpected_sprintf_token',
 532              'The %s contains %d items',
 533              'El %s contiene %d elementos'
 534          );
 535          $this->assertNoWarnings(
 536              'unexpected_sprintf_token',
 537              'The %2$s contains %1$d items. That\'s a nice %2$s full of %1$d items.',
 538              'El %2$s contiene %1$d elementos. Es un bonito %2$s lleno de %1$d elementos.'
 539          );
 540          $this->assertNoWarnings(
 541              'unexpected_sprintf_token',
 542              'The application password %friendly_name%.',
 543              'La contraseña de aplicación %friendly_name%.'
 544          );
 545  
 546          $this->assertHasWarningsAndContainsOutput(
 547              'unexpected_sprintf_token',
 548              '<a href="%d">100 percent</a>',
 549              '<a href="%d">100%</a>',
 550              'The translation contains the following unexpected placeholders: ">100%<'
 551          );
 552          $this->assertHasWarningsAndContainsOutput(
 553              'unexpected_sprintf_token',
 554              '<a href="%f">100 percent</a>',
 555              ' 95% of <a href="%f">100%%</a>',
 556              'The translation contains the following unexpected placeholders: 95% '
 557          );
 558          $this->assertHasWarningsAndContainsOutput(
 559              'unexpected_sprintf_token',
 560              '<a href="%f">100 percent</a>',
 561              '<a href="%f">100%%</a> of 95% ',
 562              'The translation contains the following unexpected placeholders: 95% '
 563          );
 564          $this->assertHasWarningsAndContainsOutput(
 565              'unexpected_sprintf_token',
 566              '<a href="%f">100 percent</a>',
 567              '<a href="%f">100%</a> of 95% ',
 568              'The translation contains the following unexpected placeholders: ">100%<, 95% '
 569          );
 570      }
 571  
 572  	function test_named_placeholders() {
 573          $this->assertNoWarnings( 'named_placeholders', '###NEW_EMAIL###', '###NEW_EMAIL###' );
 574          $this->assertNoWarnings(
 575              'named_placeholders',
 576              'Hi ###USERNAME###, we sent to ###EMAIL### your new password from "###SITENAME###" (###SITEURL###)',
 577              'Hola ###USERNAME###, te enviamos desde «###SITENAME###» (###SITEURL###) tu nueva contraseña a ###EMAIL###'
 578          );
 579  
 580          $this->assertHasWarningsAndContainsOutput(
 581              'named_placeholders',
 582              '###NEW_EMAIL###',
 583              '##NEW_EMAIL##',
 584              'The translation appears to be missing the following placeholders: ###NEW_EMAIL###'
 585          );
 586          $this->assertHasWarningsAndContainsOutput(
 587              'named_placeholders',
 588              '##NEW_EMAIL###',
 589              '###NEW_EMAIL###',
 590              'The translation contains the following unexpected placeholders: ###NEW_EMAIL###'
 591          );
 592          $this->assertHasWarningsAndContainsOutput(
 593              'named_placeholders',
 594              '###NEW_EMAIL###',
 595              '###NUEVO_CORREO###',
 596              "The translation appears to be missing the following placeholders: ###NEW_EMAIL###\nThe translation contains the following unexpected placeholders: ###NUEVO_CORREO###"
 597          );
 598          $this->assertHasWarningsAndContainsOutput(
 599              'named_placeholders',
 600              'Hi ###USERNAME###, we sent to ###EMAIL### your new password from "###SITENAME###" (###SITEURL###)',
 601              'Hola ##USERNAME##, te enviamos desde «###SITENAME###» (###SITEURL###) tu nueva contraseña a ###EMAIL###',
 602              'The translation appears to be missing the following placeholders: ###USERNAME###'
 603          );
 604          $this->assertHasWarningsAndContainsOutput(
 605              'named_placeholders',
 606              'Hi ###USERNAME###, we sent to ###EMAIL### your new password from "###SITENAME###" (###SITEURL###)',
 607              'Hola ###USERNAME###, te enviamos desde «SITENAME» (###SITEURL###) tu nueva contraseña a ###EMAIL###',
 608              'The translation appears to be missing the following placeholders: ###SITENAME###'
 609          );
 610          $this->assertHasWarningsAndContainsOutput(
 611              'named_placeholders',
 612              'Hi ###USERNAME###, we sent to ###EMAIL### your new password from "###SITENAME###" (###SITEURL###)',
 613              'Hola ###USERNAME###, te enviamos desde «###SITENAME###» (###SITEURL###) tu nueva contraseña a EMAIL#',
 614              'The translation appears to be missing the following placeholders: ###EMAIL###'
 615          );
 616      }
 617  
 618  	public function test_chained_warnings() {
 619          $this->tw = new GP_Translation_Warnings();
 620          $this->w  = new GP_Builtin_Translation_Warnings();
 621          $this->w->add_all( $this->tw );
 622  
 623          $this->assertContainsOutput(
 624              'original %1$s',
 625              'original %2$s',
 626              array( 'translation %1$s', 'translation %2$s' ),
 627              null
 628          );
 629          $this->assertContainsOutput(
 630              'original %1$s',
 631              null,
 632              array( '<a>translation %1$s' ),
 633              array(
 634                  array( 'tags' => 'Too many tags in translation. Found: <a>' ),
 635              )
 636          );
 637          $this->assertContainsOutput(
 638              'original %1$s',
 639              null,
 640              array( '<a>translation s' ),
 641              array(
 642                  array(
 643                      'tags'         => 'Too many tags in translation. Found: <a>',
 644                      'placeholders' => 'Missing %1$s placeholder in translation.',
 645                  ),
 646              )
 647          );
 648          $this->assertContainsOutput(
 649              'original',
 650              null,
 651              array( '<a>translation  %1$s' ),
 652              array(
 653                  array(
 654                      'tags'         => 'Too many tags in translation. Found: <a>',
 655                      'placeholders' => 'Extra %1$s placeholder in translation.',
 656                  ),
 657              )
 658          );
 659          $this->assertContainsOutput(
 660              'original s',
 661              null,
 662              array( "\n<a>translation s" ),
 663              array(
 664                  array(
 665                      'tags'                        => 'Too many tags in translation. Found: <a>',
 666                      'should_not_begin_on_newline' => 'Translation should not begin on newline.',
 667                  ),
 668              )
 669          );
 670          $this->assertContainsOutput(
 671              'original s',
 672              null,
 673              array( "<a>translation s\n" ),
 674              array(
 675                  array(
 676                      'tags'                      => 'Too many tags in translation. Found: <a>',
 677                      'should_not_end_on_newline' => 'Translation should not end on newline.',
 678                  ),
 679              )
 680          );
 681          $this->assertContainsOutput(
 682              "\noriginal s",
 683              null,
 684              array( '<a>translation s' ),
 685              array(
 686                  array(
 687                      'tags'                    => 'Too many tags in translation. Found: <a>',
 688                      'should_begin_on_newline' => 'Original and translation should both begin on newline.',
 689                  ),
 690              )
 691          );
 692          $this->assertContainsOutput(
 693              "original s\n",
 694              null,
 695              array( '<a>translation s' ),
 696              array(
 697                  array(
 698                      'tags'                  => 'Too many tags in translation. Found: <a>',
 699                      'should_end_on_newline' => 'Original and translation should both end on newline.',
 700                  ),
 701              )
 702          );
 703          $this->assertContainsOutput(
 704              'original',
 705              null,
 706              array( "<a>translation very very very very long \n" ),
 707              array(
 708                  array(
 709                      'tags'                      => 'Too many tags in translation. Found: <a>',
 710                      'length'                    => 'Lengths of source and translation differ too much.',
 711                      'should_not_end_on_newline' => 'Translation should not end on newline.',
 712                  ),
 713              )
 714          );
 715          $this->assertContainsOutput(
 716              '<p>original</p>',
 717              null,
 718              array( "</a>translation \n" ),
 719              array(
 720                  array(
 721                      'tags'                      => 'Missing tags from translation. Expected: <p> </p>',
 722                      'should_not_end_on_newline' => 'Translation should not end on newline.',
 723                  ),
 724              )
 725          );
 726          $this->assertContainsOutput(
 727              '<p>original</p>',
 728              null,
 729              array( "</p>translation<p> \n" ),
 730              array(
 731                  array(
 732                      'tags'                      => 'The translation contains incorrect HTML tags: Unexpected end tag : p',
 733                      'should_not_end_on_newline' => 'Translation should not end on newline.',
 734                  ),
 735              )
 736          );
 737          $this->assertContainsOutput(
 738              '<p>original</p>',
 739              null,
 740              array( "\n<a>translation</a> \n" ),
 741              array(
 742                  array(
 743                      'tags'                        => "Expected <p>, got <a>.\nExpected </p>, got </a>.",
 744                      'should_not_end_on_newline'   => 'Translation should not end on newline.',
 745                      'should_not_begin_on_newline' => 'Translation should not begin on newline.',
 746                  ),
 747              )
 748          );
 749          $this->assertContainsOutput(
 750              '<p>https://example.com</p>',
 751              null,
 752              array( "\n<a>https://example.org</a> \n" ),
 753              array(
 754                  array(
 755                      'tags'                        => "Expected <p>, got <a>.\nExpected </p>, got </a>.",
 756                      'should_not_end_on_newline'   => 'Translation should not end on newline.',
 757                      'should_not_begin_on_newline' => 'Translation should not begin on newline.',
 758                      'mismatching_urls'            => "The translation appears to be missing the following URLs: https://example.com\nThe translation contains the following unexpected URLs: https://example.org",
 759                  ),
 760              )
 761          );
 762          $this->assertContainsOutput(
 763              '<a href="https://example.com">https://example.com</a>',
 764              null,
 765              array( "\n<a href=\"https://example.org\">https://example.org</a> \n" ),
 766              array(
 767                  array(
 768                      'tags'                        => "The translation appears to be missing the following URLs: https://example.com\nThe translation contains the following unexpected URLs: https://example.org",
 769                      'should_not_end_on_newline'   => 'Translation should not end on newline.',
 770                      'should_not_begin_on_newline' => 'Translation should not begin on newline.',
 771                      'mismatching_urls'            => "The translation appears to be missing the following URLs: https://example.com\nThe translation contains the following unexpected URLs: https://example.org",
 772                  ),
 773              )
 774          );
 775          $this->assertContainsOutput(
 776              '<a href="%s">https://example.com</a>',
 777              null,
 778              array( "\n<a href=\"javascript%s\">https://example.org</a> \n" ),
 779              array(
 780                  array(
 781                      'tags'                        => 'The translation contains the following unexpected links: javascript%s',
 782                      'should_not_end_on_newline'   => 'Translation should not end on newline.',
 783                      'should_not_begin_on_newline' => 'Translation should not begin on newline.',
 784                      'mismatching_urls'            => "The translation appears to be missing the following URLs: https://example.com\nThe translation contains the following unexpected URLs: https://example.org",
 785                  ),
 786              )
 787          );
 788          $this->assertContainsOutput(
 789              '<p><a href="%s">100 percent</a></p>',
 790              null,
 791              array( "\n<a href=\"%s\">100%%</a>\n" ),
 792              array(
 793                  array(
 794                      'tags'                        => 'Missing tags from translation. Expected: <p> </p>',
 795                      'should_not_end_on_newline'   => 'Translation should not end on newline.',
 796                      'should_not_begin_on_newline' => 'Translation should not begin on newline.',
 797                      'placeholders'                => 'Extra %% placeholder in translation.',
 798                  ),
 799              )
 800          );
 801      }
 802  }


Generated: Tue Sep 21 01:01:57 2021 Cross-referenced by PHPXref 0.7.1