[ 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          $this->assertNoWarnings( 'placeholders', '%1$d baba %2$@', '%2$@ баба %1$d' );
 204          $this->assertNoWarnings( 'placeholders', '%1$.4f baba', '%1$.4f баба' );
 205          $this->assertNoWarnings( 'placeholders', '%2$.2fMB baba', '%2$.2fMB баба' );
 206          $this->assertNoWarnings( 'placeholders', 'Data: %1$.2fMB | Index: %2$.2fMB | Free: %3$.2fMB | Engine: %4$s', 'Data: %1$.2fMB | Index: %2$.2fMB | Free: %3$.2fMB | Engine: %4$s' );
 207  
 208          $this->assertHasWarningsAndContainsOutput(
 209              'placeholders',
 210              '%1$.2f baba',
 211              '%1$.f баба',
 212              'Missing %1$.2f placeholder in translation.'
 213          );
 214          $this->assertHasWarningsAndContainsOutput(
 215              'placeholders',
 216              '%1$.f baba',
 217              '%1$.4f баба',
 218              'Extra %1$.4f placeholder in translation.'
 219          );
 220          $this->assertHasWarningsAndContainsOutput(
 221              'placeholders',
 222              '%s baba',
 223              'баба',
 224              'Missing %s placeholder in translation.'
 225          );
 226          $this->assertHasWarningsAndContainsOutput(
 227              'placeholders',
 228              '%s baba',
 229              '% баба',
 230              'Missing %s placeholder in translation.'
 231          );
 232          $this->assertHasWarningsAndContainsOutput(
 233              'placeholders',
 234              '%1$s baba',
 235              'баба',
 236              'Missing %1$s placeholder in translation.'
 237          );
 238          $this->assertHasWarningsAndContainsOutput(
 239              'placeholders',
 240              '%% baba',
 241              '% баба',
 242              'Missing %% placeholder in translation.'
 243          );
 244          $this->assertHasWarningsAndContainsOutput(
 245              'placeholders',
 246              '%s baba',
 247              '%%s баба',
 248              'Missing %s placeholder in translation.'
 249          );
 250          $this->assertHasWarningsAndContainsOutput(
 251              'placeholders',
 252              '%1$s baba',
 253              '%%1$s баба',
 254              'Missing %1$s placeholder in translation.'
 255          );
 256          $this->assertHasWarningsAndContainsOutput(
 257              'placeholders',
 258              'баба',
 259              '%s baba',
 260              'Extra %s placeholder in translation.'
 261          );
 262          $this->assertHasWarningsAndContainsOutput(
 263              'placeholders',
 264              '%1$d baba %2$@',
 265              '%1$d baba',
 266              'Missing %2$@ placeholder in translation.'
 267          );
 268          $this->assertHasWarningsAndContainsOutput(
 269              'placeholders',
 270              '%1$d baba',
 271              '%1$d baba %2$@',
 272              'Extra %2$@ placeholder in translation.'
 273          );
 274      }
 275  
 276  	function test_should_begin_end_on_newline() {
 277          $this->assertHasWarningsAndContainsOutput(
 278              'should_begin_on_newline',
 279              "\nbaba",
 280              'baba',
 281              'Original and translation should both begin on newline.'
 282          );
 283          $this->assertHasWarningsAndContainsOutput(
 284              'should_not_begin_on_newline',
 285              'baba',
 286              "\nbaba",
 287              'Translation should not begin on newline.'
 288          );
 289          $this->assertHasWarningsAndContainsOutput(
 290              'should_end_on_newline',
 291              "baba\n",
 292              'baba',
 293              'Original and translation should both end on newline.'
 294          );
 295          $this->assertHasWarningsAndContainsOutput(
 296              'should_not_end_on_newline',
 297              'baba',
 298              "baba\n",
 299              'Translation should not end on newline.'
 300          );
 301  
 302          $this->assertNoWarnings( 'should_begin_on_newline', 'baba', 'baba' );
 303          $this->assertNoWarnings( 'should_not_begin_on_newline', 'baba', 'baba' );
 304          $this->assertNoWarnings( 'should_end_on_newline', 'baba', 'baba' );
 305          $this->assertNoWarnings( 'should_not_end_on_newline', 'baba', 'baba' );
 306  
 307          $this->assertNoWarnings( 'should_begin_on_newline', "baba\n", "baba\n" );
 308          $this->assertNoWarnings( 'should_not_begin_on_newline', "baba\n", "baba\n" );
 309          $this->assertNoWarnings( 'should_end_on_newline', "baba\n", "baba\n" );
 310          $this->assertNoWarnings( 'should_not_end_on_newline', "baba\n", "baba\n" );
 311  
 312          $this->assertNoWarnings( 'should_begin_on_newline', "\nbaba", "\nbaba" );
 313          $this->assertNoWarnings( 'should_not_begin_on_newline', "\nbaba", "\nbaba" );
 314          $this->assertNoWarnings( 'should_end_on_newline', "\nbaba", "\nbaba" );
 315          $this->assertNoWarnings( 'should_not_end_on_newline', "\nbaba", "\nbaba" );
 316      }
 317  
 318  	function test_placeholders_using_check() {
 319          $w       = new GP_Translation_Warnings();
 320          $builtin = new GP_Builtin_Translation_Warnings();
 321          $w->add( 'placeholder', array( $builtin, 'warning_placeholders' ) );
 322  
 323          $fr = new GP_Locale(
 324              array(
 325                  'nplurals'          => 2,
 326                  'plural_expression' => 'n > 1',
 327              )
 328          );
 329          $this->assertEquals(
 330              null,
 331              $w->check( 'original %1$s', 'original %2$s', array( 'translation %1$s', 'translation %2$s' ), $fr )
 332          );
 333          $this->assertEquals(
 334              null,
 335              $w->check( 'original %1$s', 'original %2$s', array( null ), $fr )
 336          );
 337          $this->assertEquals(
 338              null,
 339              $w->check( 'original', 'original %s', array( 'translation', 'translation %s' ), $fr )
 340          );
 341          $this->assertEquals(
 342              array( 1 => array( 'placeholder' => 'Missing %2$s placeholder in translation.' ) ),
 343              $w->check( 'original %1$s', 'original %2$s', array( 'translation %1$s', 'translation' ), $fr )
 344          );
 345          $this->assertEquals(
 346              array( 0 => array( 'placeholder' => 'Missing %1$s placeholder in translation.' ) ),
 347              $w->check( 'original %1$s', 'original %2$s', array( 'translation', 'translation  %2$s' ), $fr )
 348          );
 349  
 350          $de = new GP_Locale(
 351              array(
 352                  'nplurals'          => 2,
 353                  'plural_expression' => 'n != 1',
 354              )
 355          );
 356          $this->assertEquals(
 357              null,
 358              $w->check( 'original %1$s', 'original %2$s', array( 'translation %1$s', 'translation %2$s' ), $de )
 359          );
 360          $this->assertEquals(
 361              null,
 362              $w->check( 'original', 'original %s', array( 'translation', 'translation %s' ), $de )
 363          );
 364  
 365          $ja = new GP_Locale(
 366              array(
 367                  'nplurals'          => 1,
 368                  'plural_expression' => '0',
 369              )
 370          );
 371  
 372          $this->assertEquals(
 373              null,
 374              $w->check( 'original %1$s', 'original %2$s', array( 'translation %2$s' ), $ja )
 375          );
 376          $this->assertEquals(
 377              null,
 378              $w->check( 'original', 'original %s', array( 'translation %s' ), $ja )
 379          );
 380          $this->assertEquals(
 381              array( 0 => array( 'placeholder' => 'Missing %2$s placeholder in translation.' ) ),
 382              $w->check( 'original %1$s', 'original %2$s', array( 'translation' ), $ja )
 383          );
 384  
 385          $ru = new GP_Locale(
 386              array(
 387                  'nplurals'          => 3,
 388                  'plural_expression' => '(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)',
 389              )
 390          );
 391  
 392          $this->assertEquals(
 393              null,
 394              $w->check( 'original %1$s', 'original %2$s', array( 'translation %1$s', 'translation %2$s', 'translation 2 %2$s' ), $ru )
 395          );
 396          $this->assertEquals(
 397              null,
 398              $w->check( 'original', 'original %s', array( 'translation', 'translation 2 %s', 'translation 3 %s' ), $ru )
 399          );
 400          $this->assertEquals(
 401              array( 1 => array( 'placeholder' => 'Missing %2$s placeholder in translation.' ) ),
 402              $w->check( 'original %1$s', 'original %2$s', array( 'translation %1$s', 'translation 2', 'translation 3 %2$s' ), $ru )
 403          );
 404          $this->assertEquals(
 405              array( 2 => array( 'placeholder' => 'Missing %s placeholder in translation.' ) ),
 406              $w->check( 'original', 'original %s', array( 'translation', 'translation 2 %s', 'translation 3' ), $ru )
 407          );
 408          $this->assertEquals(
 409              array(
 410                  1 => array( 'placeholder' => 'Missing %s placeholder in translation.' ),
 411                  2 => array( 'placeholder' => 'Missing %s placeholder in translation.' ),
 412              ),
 413              $w->check( 'original', 'original %s', array( 'translation', 'translation 2', 'translation 3' ), $ru )
 414          );
 415      }
 416  
 417  	function test_mismatching_urls() {
 418          $this->assertNoWarnings( 'mismatching_urls', 'https://www.example', 'https://www.example' );
 419          $this->assertNoWarnings( 'mismatching_urls', 'http://www.example', 'http://www.example' );
 420          $this->assertNoWarnings( 'mismatching_urls', '//www.example', '//www.example' );
 421          $this->assertNoWarnings( 'mismatching_urls', '"//www.example"', '"//www.example.com"' );
 422          $this->assertNoWarnings( 'mismatching_urls', "'//www.example'", "'//www.example.com'" );
 423          $this->assertNoWarnings( 'mismatching_urls', '// www.example', '// www.example.comte    ' );
 424          $this->assertNoWarnings( 'mismatching_urls', 'http://127.0.0.1', 'https://127.0.0.1' );
 425          $this->assertNoWarnings( 'mismatching_urls', 'https://127.0.0.1', 'http://127.0.0.1' );
 426          $this->assertNoWarnings( 'mismatching_urls', 'https://www.example.com', 'https://www.example.com/' );
 427          $this->assertNoWarnings( 'mismatching_urls', 'https://www.example.com/', 'https://www.example.com' );
 428          $this->assertNoWarnings( 'mismatching_urls', 'http://www.example.com', 'https://www.example.com/' );
 429          $this->assertNoWarnings( 'mismatching_urls', 'http://www.example.com/', 'https://www.example.com' );
 430          $this->assertNoWarnings( 'mismatching_urls', 'http://wordpress.org/plugins/example-plugin/', 'https://wordpress.org/plugins/example-plugin' );
 431          $this->assertNoWarnings( 'mismatching_urls', 'https://wordpress.org/plugins/example-plugin', 'http://wordpress.org/plugins/example-plugin/' );
 432          $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' );
 433          $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' );
 434          $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  ' );
 435          $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  ' );
 436          $this->assertNoWarnings( 'mismatching_urls', 'Text1 https://www.example.com Text2 https://www.example.org Text3', '  https://www.example.org https://www.example.com ' );
 437          $this->assertNoWarnings( 'mismatching_urls', 'https://wordpress.org/plugins/example-plugin/', 'https://es.wordpress.org/plugins/example-plugin/' );
 438          $this->assertNoWarnings( 'mismatching_urls', 'https://wordpress.com/log-in/', 'https://es.wordpress.com/log-in/' );
 439          $this->assertNoWarnings( 'mismatching_urls', 'https://en.gravatar.com/matt', 'https://es.gravatar.com/matt' );
 440          $this->assertNoWarnings( 'mismatching_urls', 'https://en.wikipedia.org/wiki/WordPress', 'https://es.wikipedia.org/wiki/WordPress' );
 441  
 442          $this->assertHasWarningsAndContainsOutput(
 443              'mismatching_urls',
 444              'HTTPS://WWW.EXAMPLE',
 445              'https://www.example',
 446              "The translation appears to be missing the following URLs: HTTPS://WWW.EXAMPLE\nThe translation contains the following unexpected URLs: https://www.example"
 447          );
 448          $this->assertHasWarningsAndContainsOutput(
 449              'mismatching_urls',
 450              'https://www.example',
 451              'HTTPS://WWW.EXAMPLE',
 452              "The translation appears to be missing the following URLs: https://www.example\nThe translation contains the following unexpected URLs: HTTPS://WWW.EXAMPLE"
 453          );
 454          $this->assertHasWarningsAndContainsOutput(
 455              'mismatching_urls',
 456              'HtTpS://WwW.eXaMpLe',
 457              'https://www.example',
 458              "The translation appears to be missing the following URLs: HtTpS://WwW.eXaMpLe\nThe translation contains the following unexpected URLs: https://www.example"
 459          );
 460          $this->assertHasWarningsAndContainsOutput(
 461              'mismatching_urls',
 462              'https://www.example.com',
 463              'https://www.example.org',
 464              "The translation appears to be missing the following URLs: https://www.example.com\nThe translation contains the following unexpected URLs: https://www.example.org"
 465          );
 466          $this->assertHasWarningsAndContainsOutput(
 467              'mismatching_urls',
 468              '//www.example.com',
 469              'http://www.example.org',
 470              "The translation appears to be missing the following URLs: //www.example.com\nThe translation contains the following unexpected URLs: http://www.example.org"
 471          );
 472          $this->assertHasWarningsAndContainsOutput(
 473              'mismatching_urls',
 474              '//www.example.com',
 475              'https://www.example.org',
 476              "The translation appears to be missing the following URLs: //www.example.com\nThe translation contains the following unexpected URLs: https://www.example.org"
 477          );
 478          $this->assertHasWarningsAndContainsOutput(
 479              'mismatching_urls',
 480              'http://www.example.com',
 481              '//www.example.org',
 482              "The translation appears to be missing the following URLs: http://www.example.com\nThe translation contains the following unexpected URLs: //www.example.org"
 483          );
 484          $this->assertHasWarningsAndContainsOutput(
 485              'mismatching_urls',
 486              'https://www.example.com',
 487              '//www.example.org',
 488              "The translation appears to be missing the following URLs: https://www.example.com\nThe translation contains the following unexpected URLs: //www.example.org"
 489          );
 490          $this->assertHasWarningsAndContainsOutput(
 491              'mismatching_urls',
 492              'https://www.exañple.com',
 493              'https://www.example.com',
 494              "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"
 495          );
 496          $this->assertHasWarningsAndContainsOutput(
 497              'mismatching_urls',
 498              'https://www.example.com',
 499              'https://www.exañple.com',
 500              "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"
 501          );
 502          $this->assertHasWarningsAndContainsOutput(
 503              'mismatching_urls',
 504              'https://www.wordpress.org/plugins/example-plugin/',
 505              'https://es.wordpress.org/plugins/example-plugin/',
 506              "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/"
 507          );
 508          $this->assertHasWarningsAndContainsOutput(
 509              'mismatching_urls',
 510              'https://www.wordpress.com/log-in/',
 511              'https://es.wordpress.com/log-in/',
 512              "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/"
 513          );
 514          $this->assertHasWarningsAndContainsOutput(
 515              'mismatching_urls',
 516              'https://es.gravatar.com/matt',
 517              'https://en.gravatar.com/matt',
 518              "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"
 519          );
 520          $this->assertHasWarningsAndContainsOutput(
 521              'mismatching_urls',
 522              'https://es.wikipedia.org/wiki/WordPress',
 523              'https://en.wikipedia.org/wiki/WordPress',
 524              "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"
 525          );
 526          $this->assertHasWarningsAndContainsOutput(
 527              'mismatching_urls',
 528              'Text1 https://www.example.com Text2',
 529              'Texto1 Texto2',
 530              'The translation appears to be missing the following URLs: https://www.example.com'
 531          );
 532          $this->assertHasWarningsAndContainsOutput(
 533              'mismatching_urls',
 534              'Text1 Text2',
 535              'Texto1 https://www.example.com Texto2',
 536              'The translation contains the following unexpected URLs: https://www.example.com'
 537          );
 538          $this->assertHasWarningsAndContainsOutput(
 539              'mismatching_urls',
 540              'Text1 https://www.example.com Text2 https://www.example.org',
 541              'Texto1 https://www.example.com Texto2',
 542              'The translation appears to be missing the following URLs: https://www.example.org'
 543          );
 544          $this->assertHasWarningsAndContainsOutput(
 545              'mismatching_urls',
 546              'Text1 https://www.example.com Text2',
 547              'Texto1 https://www.example.com Texto2 https://www.example.org',
 548              'The translation contains the following unexpected URLs: https://www.example.org'
 549          );
 550      }
 551  
 552  
 553  	function test_unexpected_sprintf_token() {
 554          $this->assertNoWarnings( 'unexpected_sprintf_token', '100 percent', '100%' );
 555          $this->assertNoWarnings( 'unexpected_sprintf_token', '<a href="%a">100 percent</a>', '<a href="%a">100%</a>' );
 556          $this->assertNoWarnings( 'unexpected_sprintf_token', '<a href="%s">100 percent</a>', '<a href="%s">100%%</a>' );
 557          $this->assertNoWarnings( 'unexpected_sprintf_token', '<a href="%1$s">100 percent</a>', '<a href="%1$s">100%%</a>' );
 558          $this->assertNoWarnings( 'unexpected_sprintf_token', '%1$.2f baba', '%1$.2f баба' );
 559          $this->assertNoWarnings( 'unexpected_sprintf_token', '%1$.2f baba', '%1$.3f баба' );
 560          $this->assertNoWarnings( 'unexpected_sprintf_token', '%2$.2fMB baba', '%2$.2fMB баба' );
 561          $this->assertNoWarnings( 'unexpected_sprintf_token', '%2$.3fMB baba', '%2$.2fMB баба' );
 562          $this->assertNoWarnings( 'unexpected_sprintf_token', 'Data: %1$.2fMB | Index: %2$.2fMB | Free: %3$.2fMB | Engine: %4$s', 'Data: %1$.2fMB | Index: %2$.2fMB | Free: %3$.2fMB | Engine: %4$s' );
 563  
 564          $this->assertNoWarnings(
 565              'unexpected_sprintf_token',
 566              'The %s contains %d items',
 567              'El %s contiene %d elementos'
 568          );
 569          $this->assertNoWarnings(
 570              'unexpected_sprintf_token',
 571              'The %2$s contains %1$d items. That\'s a nice %2$s full of %1$d items.',
 572              'El %2$s contiene %1$d elementos. Es un bonito %2$s lleno de %1$d elementos.'
 573          );
 574          $this->assertNoWarnings(
 575              'unexpected_sprintf_token',
 576              'The application password %friendly_name%.',
 577              'La contraseña de aplicación %friendly_name%.'
 578          );
 579  
 580          $this->assertHasWarningsAndContainsOutput(
 581              'unexpected_sprintf_token',
 582              '<a href="%d">100 percent</a>',
 583              '<a href="%d">100%</a>',
 584              'The translation contains the following unexpected placeholders: ">100%<'
 585          );
 586          $this->assertHasWarningsAndContainsOutput(
 587              'unexpected_sprintf_token',
 588              '<a href="%f">100 percent</a>',
 589              ' 95% of <a href="%f">100%%</a>',
 590              'The translation contains the following unexpected placeholders: 95% '
 591          );
 592          $this->assertHasWarningsAndContainsOutput(
 593              'unexpected_sprintf_token',
 594              '<a href="%f">100 percent</a>',
 595              '<a href="%f">100%%</a> of 95% ',
 596              'The translation contains the following unexpected placeholders: 95% '
 597          );
 598          $this->assertHasWarningsAndContainsOutput(
 599              'unexpected_sprintf_token',
 600              '<a href="%f">100 percent</a>',
 601              '<a href="%f">100%</a> of 95% ',
 602              'The translation contains the following unexpected placeholders: ">100%<, 95% '
 603          );
 604      }
 605  
 606  	function test_named_placeholders() {
 607          $this->assertNoWarnings( 'named_placeholders', '###NEW_EMAIL###', '###NEW_EMAIL###' );
 608          $this->assertNoWarnings( 'named_placeholders', '###new-email###', '###new-email###' );
 609          $this->assertNoWarnings(
 610              'named_placeholders',
 611              'Hi ###USERNAME###, we sent to ###EMAIL### your new password from "###SITENAME###" (###SITEURL###)',
 612              'Hola ###USERNAME###, te enviamos desde «###SITENAME###» (###SITEURL###) tu nueva contraseña a ###EMAIL###'
 613          );
 614  
 615          $this->assertHasWarningsAndContainsOutput(
 616              'named_placeholders',
 617              '###NEW_EMAIL###',
 618              '##NEW_EMAIL##',
 619              'The translation appears to be missing the following placeholders: ###NEW_EMAIL###'
 620          );
 621          $this->assertHasWarningsAndContainsOutput(
 622              'named_placeholders',
 623              '##NEW_EMAIL###',
 624              '###NEW_EMAIL###',
 625              'The translation contains the following unexpected placeholders: ###NEW_EMAIL###'
 626          );
 627          $this->assertHasWarningsAndContainsOutput(
 628              'named_placeholders',
 629              '###NEW_EMAIL###',
 630              '###NUEVO_CORREO###',
 631              "The translation appears to be missing the following placeholders: ###NEW_EMAIL###\nThe translation contains the following unexpected placeholders: ###NUEVO_CORREO###"
 632          );
 633          $this->assertHasWarningsAndContainsOutput(
 634              'named_placeholders',
 635              'Hi ###USERNAME###, we sent to ###EMAIL### your new password from "###SITENAME###" (###SITEURL###)',
 636              'Hola ##USERNAME##, te enviamos desde «###SITENAME###» (###SITEURL###) tu nueva contraseña a ###EMAIL###',
 637              'The translation appears to be missing the following placeholders: ###USERNAME###'
 638          );
 639          $this->assertHasWarningsAndContainsOutput(
 640              'named_placeholders',
 641              'Hi ###USERNAME###, we sent to ###EMAIL### your new password from "###SITENAME###" (###SITEURL###)',
 642              'Hola ###USERNAME###, te enviamos desde «SITENAME» (###SITEURL###) tu nueva contraseña a ###EMAIL###',
 643              'The translation appears to be missing the following placeholders: ###SITENAME###'
 644          );
 645          $this->assertHasWarningsAndContainsOutput(
 646              'named_placeholders',
 647              'Hi ###USERNAME###, we sent to ###EMAIL### your new password from "###SITENAME###" (###SITEURL###)',
 648              'Hola ###USERNAME###, te enviamos desde «###SITENAME###» (###SITEURL###) tu nueva contraseña a EMAIL#',
 649              'The translation appears to be missing the following placeholders: ###EMAIL###'
 650          );
 651          $this->assertHasWarningsAndContainsOutput(
 652              'named_placeholders',
 653              '###NEW_EMAIL###',
 654              '###new_email###',
 655              "The translation appears to be missing the following placeholders: ###NEW_EMAIL###\nThe translation contains the following unexpected placeholders: ###new_email###"
 656          );
 657          $this->assertHasWarningsAndContainsOutput(
 658              'named_placeholders',
 659              '###new_email###',
 660              '###NEW_EMAIL###',
 661              "The translation appears to be missing the following placeholders: ###new_email###\nThe translation contains the following unexpected placeholders: ###NEW_EMAIL###"
 662          );
 663      }
 664  
 665  	public function test_chained_warnings() {
 666          $this->tw = new GP_Translation_Warnings();
 667          $this->w  = new GP_Builtin_Translation_Warnings();
 668          $this->w->add_all( $this->tw );
 669  
 670          $this->assertContainsOutput(
 671              'original %1$s',
 672              'original %2$s',
 673              array( 'translation %1$s', 'translation %2$s' ),
 674              null
 675          );
 676          $this->assertContainsOutput(
 677              'original %1$s',
 678              null,
 679              array( '<a>translation %1$s' ),
 680              array(
 681                  array( 'tags' => 'Too many tags in translation. Found: <a>' ),
 682              )
 683          );
 684          $this->assertContainsOutput(
 685              'original %1$s',
 686              null,
 687              array( '<a>translation s' ),
 688              array(
 689                  array(
 690                      'tags'         => 'Too many tags in translation. Found: <a>',
 691                      'placeholders' => 'Missing %1$s placeholder in translation.',
 692                  ),
 693              )
 694          );
 695          $this->assertContainsOutput(
 696              'original',
 697              null,
 698              array( '<a>translation  %1$s' ),
 699              array(
 700                  array(
 701                      'tags'         => 'Too many tags in translation. Found: <a>',
 702                      'placeholders' => 'Extra %1$s placeholder in translation.',
 703                  ),
 704              )
 705          );
 706          $this->assertContainsOutput(
 707              'original s',
 708              null,
 709              array( "\n<a>translation s" ),
 710              array(
 711                  array(
 712                      'tags'                        => 'Too many tags in translation. Found: <a>',
 713                      'should_not_begin_on_newline' => 'Translation should not begin on newline.',
 714                  ),
 715              )
 716          );
 717          $this->assertContainsOutput(
 718              'original s',
 719              null,
 720              array( "<a>translation s\n" ),
 721              array(
 722                  array(
 723                      'tags'                      => 'Too many tags in translation. Found: <a>',
 724                      'should_not_end_on_newline' => 'Translation should not end on newline.',
 725                  ),
 726              )
 727          );
 728          $this->assertContainsOutput(
 729              "\noriginal s",
 730              null,
 731              array( '<a>translation s' ),
 732              array(
 733                  array(
 734                      'tags'                    => 'Too many tags in translation. Found: <a>',
 735                      'should_begin_on_newline' => 'Original and translation should both begin on newline.',
 736                  ),
 737              )
 738          );
 739          $this->assertContainsOutput(
 740              "original s\n",
 741              null,
 742              array( '<a>translation s' ),
 743              array(
 744                  array(
 745                      'tags'                  => 'Too many tags in translation. Found: <a>',
 746                      'should_end_on_newline' => 'Original and translation should both end on newline.',
 747                  ),
 748              )
 749          );
 750          $this->assertContainsOutput(
 751              'original',
 752              null,
 753              array( "<a>translation very very very very long \n" ),
 754              array(
 755                  array(
 756                      'tags'                      => 'Too many tags in translation. Found: <a>',
 757                      'length'                    => 'Lengths of source and translation differ too much.',
 758                      'should_not_end_on_newline' => 'Translation should not end on newline.',
 759                  ),
 760              )
 761          );
 762          $this->assertContainsOutput(
 763              '<p>original</p>',
 764              null,
 765              array( "</a>translation \n" ),
 766              array(
 767                  array(
 768                      'tags'                      => 'Missing tags from translation. Expected: <p> </p>',
 769                      'should_not_end_on_newline' => 'Translation should not end on newline.',
 770                  ),
 771              )
 772          );
 773          $this->assertContainsOutput(
 774              '<p>original</p>',
 775              null,
 776              array( "</p>translation<p> \n" ),
 777              array(
 778                  array(
 779                      'tags'                      => 'The translation contains incorrect HTML tags: Unexpected end tag : p',
 780                      'should_not_end_on_newline' => 'Translation should not end on newline.',
 781                  ),
 782              )
 783          );
 784          $this->assertContainsOutput(
 785              '<p>original</p>',
 786              null,
 787              array( "\n<a>translation</a> \n" ),
 788              array(
 789                  array(
 790                      'tags'                        => "Expected <p>, got <a>.\nExpected </p>, got </a>.",
 791                      'should_not_end_on_newline'   => 'Translation should not end on newline.',
 792                      'should_not_begin_on_newline' => 'Translation should not begin on newline.',
 793                  ),
 794              )
 795          );
 796          $this->assertContainsOutput(
 797              '<p>https://example.com</p>',
 798              null,
 799              array( "\n<a>https://example.org</a> \n" ),
 800              array(
 801                  array(
 802                      'tags'                        => "Expected <p>, got <a>.\nExpected </p>, got </a>.",
 803                      'should_not_end_on_newline'   => 'Translation should not end on newline.',
 804                      'should_not_begin_on_newline' => 'Translation should not begin on newline.',
 805                      'mismatching_urls'            => "The translation appears to be missing the following URLs: https://example.com\nThe translation contains the following unexpected URLs: https://example.org",
 806                  ),
 807              )
 808          );
 809          $this->assertContainsOutput(
 810              '<a href="https://example.com">https://example.com</a>',
 811              null,
 812              array( "\n<a href=\"https://example.org\">https://example.org</a> \n" ),
 813              array(
 814                  array(
 815                      'tags'                        => "The translation appears to be missing the following URLs: https://example.com\nThe translation contains the following unexpected URLs: https://example.org",
 816                      'should_not_end_on_newline'   => 'Translation should not end on newline.',
 817                      'should_not_begin_on_newline' => 'Translation should not begin on newline.',
 818                      'mismatching_urls'            => "The translation appears to be missing the following URLs: https://example.com\nThe translation contains the following unexpected URLs: https://example.org",
 819                  ),
 820              )
 821          );
 822          $this->assertContainsOutput(
 823              '<a href="%s">https://example.com</a>',
 824              null,
 825              array( "\n<a href=\"javascript%s\">https://example.org</a> \n" ),
 826              array(
 827                  array(
 828                      'tags'                        => 'The translation contains the following unexpected links: javascript%s',
 829                      'should_not_end_on_newline'   => 'Translation should not end on newline.',
 830                      'should_not_begin_on_newline' => 'Translation should not begin on newline.',
 831                      'mismatching_urls'            => "The translation appears to be missing the following URLs: https://example.com\nThe translation contains the following unexpected URLs: https://example.org",
 832                  ),
 833              )
 834          );
 835          $this->assertContainsOutput(
 836              '<p><a href="%s">100 percent</a></p>',
 837              null,
 838              array( "\n<a href=\"%s\">100%%</a>\n" ),
 839              array(
 840                  array(
 841                      'tags'                        => 'Missing tags from translation. Expected: <p> </p>',
 842                      'should_not_end_on_newline'   => 'Translation should not end on newline.',
 843                      'should_not_begin_on_newline' => 'Translation should not begin on newline.',
 844                      'placeholders'                => 'Extra %% placeholder in translation.',
 845                  ),
 846              )
 847          );
 848      }
 849  }


Generated: Fri Apr 26 01:01:24 2024 Cross-referenced by PHPXref 0.7.1