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


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