[ 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 ) ) $locale = $this->l;
  15          $method = "warning_$warning";
  16          $this->$assert( true, $this->w->$method( $original, $translation, $locale ) );
  17      }
  18  
  19  	function assertHasWarnings( $warning, $original, $translation, $locale = null ) {
  20          $this->_assertWarning( 'assertNotSame', $warning, $original, $translation, $locale );
  21      }
  22  
  23  	function assertNoWarnings( $warning, $original, $translation, $locale = null ) {
  24          $this->_assertWarning( 'assertSame', $warning, $original, $translation, $locale );
  25      }
  26  
  27  
  28  	function test_length() {
  29          $this->assertNoWarnings( 'length', $this->longer_than_20, $this->longer_than_20 );
  30          $this->assertHasWarnings( 'length', $this->longer_than_20, $this->shorter_than_5 );
  31      }
  32  
  33  	function test_length_exclude() {
  34          $w_without_locale = new GP_Builtin_Translation_Warnings;
  35          $w_without_locale->length_exclude_languages = array( $this->l->slug );
  36          $this->assertSame( true, $w_without_locale->warning_length( $this->longer_than_20, $this->longer_than_20, $this->l ) );
  37          $this->assertSame( true, $w_without_locale->warning_length( $this->longer_than_20, $this->shorter_than_5, $this->l ) );
  38      }
  39  
  40  	function test_tags() {
  41          $this->assertNoWarnings( 'tags', 'Baba', 'Баба' );
  42          $this->assertNoWarnings( 'tags', '<a href="%s">Baba</a>', '<a href="%s">Баба</a>' );
  43          $this->assertNoWarnings( 'tags', '<a href="%s" title="Blimp!">Baba</a>', '<a href="%s" title="Блимп!">Баба</a>' );
  44          $this->assertNoWarnings( 'tags', '<a href="%s" aria-label="Blimp!">Baba</a>', '<a href="%s" aria-label="Блимп!">Баба</a>' );
  45          $this->assertNoWarnings( 'tags', '<a href="%s" title="Blimp!" aria-label="Blimp!">Baba</a>', '<a href="%s" title="Блимп!" aria-label="Блимп!">Баба</a>' );
  46  
  47          $this->assertHasWarnings( 'tags', '<a href="%s" title="Blimp!">Baba</a>', '<a href="javascript:%s" title="Блимп!">Баба</a>' );
  48          $this->assertHasWarnings( 'tags', '<a href="%s" title="Blimp!">Baba</a>', '<a href="%s" x>Баба</a>' );
  49          $this->assertHasWarnings( 'tags', '<a href="%s" title="Blimp!">Baba</a>', '<a href="javascript:%s" title="Блимп!" target="_blank">Баба</a>' );
  50          $this->assertHasWarnings( 'tags', '<a>Baba</a>', '</a>Баба<a>' );
  51      }
  52  
  53  	function test_add_all() {
  54          $warnings = $this->getMockBuilder('GP_Translation_Warnings')->getMock();
  55          // we check for the number of warnings, because PHPUnit doesn't allow
  56          // us to check if each argument is a callable
  57          $warnings->expects( $this->exactly( 7 ) )->method( 'add' )->will( $this->returnValue( true ) );
  58          $this->w->add_all( $warnings );
  59      }
  60  
  61  	function test_placeholders() {
  62          $this->assertHasWarnings( 'placeholders', '%s baba', 'баба' );
  63          $this->assertHasWarnings( 'placeholders', '%s baba', '% баба' );
  64          $this->assertNoWarnings( 'placeholders', '%s baba', '%s баба' );
  65          $this->assertNoWarnings( 'placeholders', '%s baba', 'баба %s' );
  66          $this->assertNoWarnings( 'placeholders', '%s baba', 'баба %s' );
  67          $this->assertNoWarnings( 'placeholders', '%1$s baba %2$s dyado', '%1$sбабадядо%2$s' );
  68          $this->assertNoWarnings( 'placeholders', '% baba', 'баба' );
  69          $this->assertNoWarnings( 'placeholders', '% baba', '% баба' );
  70          $this->assertHasWarnings( 'placeholders', '%1$s baba', 'баба' );
  71          $this->assertNoWarnings( 'placeholders', '%1$s baba', '%1$s баба' );
  72          $this->assertNoWarnings( 'placeholders', '%sHome%s', '%sНачало%s' );
  73          $this->assertNoWarnings( 'placeholders', 'This string has %stwo variables%s.', 'Deze string heeft %stwee variabelen%s.' );
  74      }
  75  
  76  	function test_should_begin_end_on_newline() {
  77          $this->assertHasWarnings( 'should_begin_on_newline', "\nbaba", "baba" );
  78          $this->assertHasWarnings( 'should_not_begin_on_newline', "baba", "\nbaba" );
  79          $this->assertHasWarnings( 'should_end_on_newline', "baba\n", "baba" );
  80          $this->assertHasWarnings( 'should_not_end_on_newline', "baba", "baba\n" );
  81  
  82          $this->assertNoWarnings( 'should_begin_on_newline', "baba", "baba" );
  83          $this->assertNoWarnings( 'should_not_begin_on_newline', "baba", "baba" );
  84          $this->assertNoWarnings( 'should_end_on_newline', "baba", "baba" );
  85          $this->assertNoWarnings( 'should_not_end_on_newline', "baba", "baba" );
  86  
  87          $this->assertNoWarnings( 'should_begin_on_newline', "baba\n", "baba\n" );
  88          $this->assertNoWarnings( 'should_not_begin_on_newline', "baba\n", "baba\n" );
  89          $this->assertNoWarnings( 'should_end_on_newline', "baba\n", "baba\n" );
  90          $this->assertNoWarnings( 'should_not_end_on_newline', "baba\n", "baba\n" );
  91  
  92          $this->assertNoWarnings( 'should_begin_on_newline', "\nbaba", "\nbaba" );
  93          $this->assertNoWarnings( 'should_not_begin_on_newline', "\nbaba", "\nbaba" );
  94          $this->assertNoWarnings( 'should_end_on_newline', "\nbaba", "\nbaba" );
  95          $this->assertNoWarnings( 'should_not_end_on_newline', "\nbaba", "\nbaba" );
  96      }
  97  
  98  	function test_placeholders_using_check() {
  99          $w = new GP_Translation_Warnings;
 100          $builtin = new GP_Builtin_Translation_Warnings;
 101          $w->add( 'placeholder', array( $builtin, 'warning_placeholders' ) );
 102  
 103          $fr = new GP_Locale( array(
 104              'nplurals' => 2,
 105              'plural_expression' => 'n > 1'
 106          ));
 107          $this->assertEquals( null,
 108              $w->check( 'original %1$s', 'original %2$s', array( 'translation %1$s', 'translation %2$s' ), $fr ) );
 109          $this->assertEquals( null,
 110              $w->check( 'original', 'original %s', array( 'translation', 'translation %s' ), $fr ) );
 111          $this->assertEquals( array( 1 => array( 'placeholder' => 'Missing %2$s placeholder in translation.' ) ),
 112              $w->check( 'original %1$s', 'original %2$s', array( 'translation %1$s', 'translation' ), $fr ) );
 113  
 114          $de = new GP_Locale( array(
 115              'nplurals' => 2,
 116              'plural_expression' => 'n != 1'
 117          ));
 118          $this->assertEquals( null,
 119              $w->check( 'original %1$s', 'original %2$s', array( 'translation %1$s', 'translation %2$s' ), $de ) );
 120          $this->assertEquals( null,
 121              $w->check( 'original', 'original %s', array( 'translation', 'translation %s' ), $de ) );
 122  
 123          $ja = new GP_Locale( array(
 124              'nplurals' => 1,
 125              'plural_expression' => '0'
 126          ));
 127  
 128          $this->assertEquals( null,
 129              $w->check( 'original %1$s', 'original %2$s', array( 'translation %2$s' ), $ja ) );
 130          $this->assertEquals( null,
 131              $w->check( 'original', 'original %s', array( 'translation %s' ), $ja ) );
 132          $this->assertEquals( array( 0 => array( 'placeholder' => 'Missing %2$s placeholder in translation.' ) ),
 133              $w->check( 'original %1$s', 'original %2$s', array( 'translation' ), $ja ) );
 134  
 135          $ru = new GP_Locale( array(
 136              'nplurals' => 3,
 137              'plural_expression' => '(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)'
 138          ));
 139  
 140          $this->assertEquals( null,
 141              $w->check( 'original %1$s', 'original %2$s', array( 'translation %1$s', 'translation %2$s', 'translation 2 %2$s' ), $ru ) );
 142          $this->assertEquals( null,
 143              $w->check( 'original', 'original %s', array( 'translation', 'translation 2 %s', 'translation 3 %s' ), $ru ) );
 144          $this->assertEquals( array( 1 => array( 'placeholder' => 'Missing %2$s placeholder in translation.' ) ),
 145              $w->check( 'original %1$s', 'original %2$s', array( 'translation %1$s', 'translation 2', 'translation 3 %2$s' ), $ru ) );
 146          $this->assertEquals( array( 2 => array( 'placeholder' => 'Missing %s placeholder in translation.' ) ),
 147              $w->check( 'original', 'original %s', array( 'translation', 'translation 2 %s', 'translation 3' ), $ru ) );
 148          $this->assertEquals( array( 1 => array( 'placeholder' => 'Missing %s placeholder in translation.' ),
 149              2 => array( 'placeholder' => 'Missing %s placeholder in translation.' ) ),
 150              $w->check( 'original', 'original %s', array( 'translation', 'translation 2', 'translation 3' ), $ru ) );
 151      }
 152  }


Generated: Mon Nov 18 01:01:56 2019 Cross-referenced by PHPXref 0.7.1