[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/tests/phpunit/testcases/tests_things/ -> test_thing_glossary_entry.php (source)

   1  <?php
   2  
   3  class GP_Test_Glossary_Entry extends GP_UnitTestCase {
   4  
   5  	function test_empty_glossary_id() {
   6          $glossary_entry = GP::$glossary_entry->create( array( 'glossary_id' => '', 'term' => 'term', 'part_of_speech' => 'verb', 'last_edited_by' =>'1' ) );
   7          $verdict = $glossary_entry->validate();
   8  
   9          $this->assertFalse( $verdict );
  10      }
  11  
  12  	function test_empty_term() {
  13          $glossary_entry = GP::$glossary_entry->create( array( 'glossary_id' => '1', 'term' => '', 'part_of_speech' => 'verb', 'last_edited_by' =>'1' ) );
  14          $verdict = $glossary_entry->validate();
  15  
  16          $this->assertFalse( $verdict );
  17      }
  18  
  19  	function test_empty_part_of_speech() {
  20          $glossary_entry = GP::$glossary_entry->create( array( 'glossary_id' => '1', 'term' => 'term', 'part_of_speech' => '', 'last_edited_by' =>'1' ) );
  21          $verdict = $glossary_entry->validate();
  22  
  23          $this->assertFalse( $verdict );
  24      }
  25  
  26  	function test_invalid_part_of_speech() {
  27          $glossary_entry = GP::$glossary_entry->create( array( 'glossary_id' => '1', 'term' => 'term', 'part_of_speech' => 'invalid', 'last_edited_by' =>'1' ) );
  28          $verdict = $glossary_entry->validate();
  29  
  30          $this->assertFalse( $verdict );
  31      }
  32  
  33  	function test_negative_last_edited_by() {
  34          $glossary_entry = GP::$glossary_entry->create( array( 'glossary_id' => '1', 'term' => 'tern', 'part_of_speech' => 'verb', 'last_edited_by' =>'-1' ) );
  35          $verdict = $glossary_entry->validate();
  36  
  37          $this->assertFalse( $verdict );
  38      }
  39  
  40  	function test_empty_last_edited_by() {
  41          $glossary_entry = GP::$glossary_entry->create( array( 'glossary_id' => '1', 'term' => 'tern', 'part_of_speech' => 'verb', 'last_edited_by' =>'0' ) );
  42          $verdict = $glossary_entry->validate();
  43  
  44          $this->assertFalse( $verdict );
  45      }
  46  
  47  	function test_by_glossary_id() {
  48          $glossary_entry_1 = GP::$glossary_entry->create( array( 'glossary_id' => '1', 'term' => 'term', 'part_of_speech' => 'verb', 'last_edited_by' =>'1' ) );
  49          $glossary_entry_2 = GP::$glossary_entry->create( array( 'glossary_id' => '2', 'term' => 'term', 'part_of_speech' => 'verb', 'last_edited_by' =>'1' ) );
  50          $new = GP::$glossary_entry->by_glossary_id( '1' );
  51          $this->assertEquals( array( $glossary_entry_1 ), $new );
  52          $this->assertNotEquals( array( $glossary_entry_2 ), $new );
  53      }
  54  
  55  	function test_part_of_speech_array_set() {
  56          $this->assertCount( 10, GP::$glossary_entry->parts_of_speech );
  57          $this->assertArrayHasKey( 'noun', GP::$glossary_entry->parts_of_speech );
  58      }
  59  
  60  	function test_delete() {
  61          $entry = GP::$glossary_entry->create( array( 'glossary_id' => '1', 'term' => 'term', 'part_of_speech' => 'verb', 'last_edited_by' =>'1' ) );
  62  
  63          $pre_delete = GP::$glossary_entry->find_one( array( 'id' => $entry->id ) );
  64  
  65          $entry->delete();
  66  
  67          $post_delete = GP::$glossary_entry->find_one( array( 'id' => $entry->id ) );
  68  
  69          $this->assertFalse( empty( $pre_delete ) );
  70          $this->assertNotEquals( $pre_delete, $post_delete );
  71      }
  72  
  73  	function test_mapping_entries_to_originals() {
  74          require_once GP_TMPL_PATH . 'helper-functions.php';
  75  
  76          $set = $this->factory->translation_set->create_with_project_and_locale();
  77          $glossary = GP::$glossary->create_and_select( array( 'translation_set_id' => $set->id ) );
  78  
  79          $nouns = array( 'term', 'box', 'city', 'toy', 'wife', 'shelf', 'man', 'woman', 'post', 'install/installation', 'color' );
  80          foreach ( $nouns as $noun ) {
  81              GP::$glossary_entry->create( array( 'glossary_id' => $glossary->id, 'term' => $noun, 'part_of_speech' => 'noun', 'translation' => $noun, 'comment' => 'my comment', 'last_edited_by' =>'1' ) );
  82          }
  83          $verbs = array( 'write', 'post' );
  84          foreach ( $verbs as $verb ) {
  85              GP::$glossary_entry->create( array( 'glossary_id' => $glossary->id, 'term' => $verb, 'part_of_speech' => 'verb', 'translation' => $verb, 'comment' => 'my comment', 'last_edited_by' =>'1' ) );
  86          }
  87  
  88          $originals = array(
  89              'term' => array( 'term' ),
  90              'terms' => array( 'term' ),
  91              'A sentence with a term to be found.' => array( 'term' ),
  92              'A sentence with some terms to be found.' => array( 'term' ),
  93              'A sentence with just a box.' => array( 'box' ),
  94              'A sentence that contains a few boxes.' => array( 'box' ),
  95              'A sentence about a city with some boxes.' => array( 'city', 'box' ),
  96              'A blog about a city.' => array( 'city' ),
  97              'Two blogs about two cities.' => array( 'city' ),
  98              'A blog about a toy.' => array( 'toy' ),
  99              'Two blogs about two toys.' => array( 'toy' ),
 100              'A blog about a shelf.' => array( 'shelf' ),
 101              'Two blogs about two shelves.' => array( 'shelf' ),
 102              'A blog about a wife.' => array( 'wife' ),
 103              'Two blogs about two wives.' => array( 'wife' ),
 104              'A blog about a man and a woman.' => array( 'man', 'woman' ),
 105              'Two blogs about two men and two women.' => array( 'man', 'woman' ),
 106              'I write about something.' => array( 'write' ),
 107              'Someone writes about something.' => array( 'write' ),
 108              'I post about something.' => array( 'post' ),
 109              'Someone posts something.' => array( 'post' ),
 110              'The Post menu should be displayed.' => array( 'post' ), // Make sure glossary entries are matched case insensitivly.
 111              'City water and power were out of service all day.' => array( 'city' ), // Make sure glossary entries are matched case insensitivly.
 112              'Write what you know and you\'ll never go wrong.' => array( 'write' ), // Make sure glossary entries are matched case insensitivly.
 113              'Posting glossary entries can be fun!' => array( 'post' ), // Make sure glossary entries detect the 'ing' suffix.
 114              'Posted glossary entries are a drag!' => array( 'post' ), // Make sure glossary entries detect the 'ed' suffix.
 115              'The green color of that leaf is quite nice.  But I like it when the fall colors are out in orange and reds!' => array( 'color' ), // Make sure the glossary term is matched in all instances within the translation.
 116          );
 117  
 118          // The number of times the glossary term for each of the originals should match.
 119          $match_count = array(
 120              'term' => 1,
 121              'terms' => 1,
 122              'A sentence with a term to be found.' => 1,
 123              'A sentence with some terms to be found.' => 1,
 124              'A sentence with just a box.' => 1,
 125              'A sentence that contains a few boxes.' => 1,
 126              'A sentence about a city with some boxes.' => 1,
 127              'A blog about a city.' => 1,
 128              'Two blogs about two cities.' => 1,
 129              'A blog about a toy.' => 1,
 130              'Two blogs about two toys.' => 1,
 131              'A blog about a shelf.' => 1,
 132              'Two blogs about two shelves.' => 1,
 133              'A blog about a wife.' => 1,
 134              'Two blogs about two wives.' => 1,
 135              'A blog about a man and a woman.' => 1,
 136              'Two blogs about two men and two women.' => 1,
 137              'I write about something.' => 1,
 138              'Someone writes about something.' => 1,
 139              'I post about something.' => 1,
 140              'Someone posts something.' => 1,
 141              'The Post menu should be displayed.' => 1,
 142              'City water and power were out of service all day.' => 1,
 143              'Write what you know and you\'ll never go wrong.' => 1,
 144              'Posting glossary entries can be fun!' => 1,
 145              'Posted glossary entries are a drag!' => 1,
 146              'The green color of that leaf is quite nice.  But I like it when the fall colors are out in orange and reds!' => 2,
 147          );
 148  
 149          foreach ( $originals as $original => $terms ) {
 150              $this->factory->original->create( array( 'project_id' => $set->project->id, 'status' => '+active', 'singular' => $original, 'plural' => $original, ) );
 151          }
 152  
 153          $translations = array();
 154          $temp_translations = GP::$translation->for_translation( $set->project, $set, 'no-limit' );
 155          foreach ( $temp_translations as $t ) {
 156              $translations[] = map_glossary_entries_to_translation_originals( $t, $glossary );
 157          }
 158  
 159          foreach ( $translations as $translation ) {
 160              foreach ( $originals[ $translation->singular ] as $term ) {
 161                  foreach ( array( 'noun', 'verb' ) as $pos ) {
 162                      if ( ! in_array( $term, ${ $pos . 's' } ) ) {
 163                          continue;
 164                      }
 165  
 166                      // echo 'Checking for ', $pos, ' ', $term, ' in ', $translation->singular, PHP_EOL;
 167                      $translation_json = array(
 168                          'translation' => $term,
 169                          'pos' => $pos,
 170                      );
 171  
 172                      $regex = '#<span class="glossary-word" data-translations="\[.*?' . preg_quote( htmlspecialchars( substr( json_encode( $translation_json ), 0, -2 ) ), '#' ) . '[^"]+">[^<]+</span>#';
 173  
 174                      $this->assertRegExp( $regex, $translation->singular_glossary_markup, 'Glossary term "' . $term . '" should have been found in "' . $translation->singular . '".' );
 175                      $this->assertRegExp( $regex, $translation->plural_glossary_markup, 'Glossary term "' . $term . '" should have been found in "' . $translation->plural . '".' );
 176                      $this->assertEquals( preg_match_all( $regex, $translation->singular_glossary_markup, $matches ), $match_count[ $translation->singular ] );
 177                      $this->assertEquals( preg_match_all( $regex, $translation->plural_glossary_markup, $matches ), $match_count[ $translation->singular ] );
 178                  }
 179              }
 180          }
 181      }
 182  }


Generated: Sun Nov 24 01:01:03 2024 Cross-referenced by PHPXref 0.7.1