[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

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

   1  <?php
   2  
   3  class GP_Test_Glossary extends GP_UnitTestCase {
   4  
   5  	function test_empty_translation_set_id() {
   6          $glossary = GP::$glossary->create_and_select( array( 'translation_set_id' => '' ) );
   7          $verdict = $glossary->validate();
   8  
   9          $this->assertFalse( $verdict );
  10      }
  11  
  12  	function test_by_set_id() {
  13          $glossary_1 = GP::$glossary->create_and_select( array( 'translation_set_id' => 1 ) );
  14          $glossary_2 = GP::$glossary->create_and_select( array( 'translation_set_id' => 2 ) );
  15          $new = GP::$glossary->by_set_id( 1 );
  16          $this->assertEquals( $glossary_1, $new );
  17          $this->assertNotEquals( $glossary_2, $new );
  18      }
  19  
  20  	function test_by_set_or_parent_project() {
  21          $locale = $this->factory->locale->create( array( 'slug' => 'bg' ) );
  22  
  23          $root = $this->factory->project->create( array( 'name' => 'root' ) );
  24          $root_set = $this->factory->translation_set->create( array( 'project_id' => $root->id, 'locale' => $locale->slug ) );
  25  
  26          $sub =     $this->factory->project->create( array( 'name' => 'sub', 'parent_project_id' => $root->id ) );
  27          $sub_set = $this->factory->translation_set->create( array( 'project_id' => $sub->id, 'locale' => $locale->slug ) );
  28  
  29          $subsub =     $this->factory->project->create( array( 'name' => 'subsub', 'parent_project_id' => $sub->id ) );
  30          $subsub_set = $this->factory->translation_set->create( array( 'project_id' => $subsub->id, 'locale' => $locale->slug ) );
  31  
  32          $glossary = GP::$glossary->create_and_select( array( 'translation_set_id' => $root_set->id ) );
  33  
  34          $sub_glossary = GP::$glossary->by_set_or_parent_project( $sub_set, $sub );
  35          $this->assertEquals( $glossary, $sub_glossary );
  36          $this->assertEquals( $glossary->path(), $sub_glossary->path() );
  37  
  38          $subsub_glossary = GP::$glossary->by_set_or_parent_project( $subsub_set, $subsub );
  39          $this->assertEquals( $glossary, $subsub_glossary );
  40          $this->assertEquals( $glossary->path(), $subsub_glossary->path() );
  41      }
  42  
  43  	function test_delete() {
  44          $glossary = GP::$glossary->create_and_select( array( 'translation_set_id' => 1 ) );
  45          $glossary->delete();
  46          $new = GP::$glossary->by_set_id( 1 );
  47          $this->assertNotEquals( $glossary, $new );
  48  
  49      }
  50  
  51      /**
  52       * @ticket gh-435
  53       */
  54  	function test_get_entries() {
  55  
  56          $this->assertFalse( GP::$glossary->get_entries() );
  57  
  58          $set = $this->factory->translation_set->create_with_project_and_locale();
  59          $glossary = GP::$glossary->create_and_select( array( 'translation_set_id' => $set->id ) );
  60  
  61          $test_entries = array(
  62              array(
  63                  'term' => 'Term',
  64                  'translation' => 'Translation',
  65                  'glossary_id' => $glossary->id,
  66              ),
  67              array(
  68                  'term' => 'Term 2',
  69                  'translation' => 'Translation 2',
  70                  'glossary_id' => $glossary->id,
  71              ),
  72          );
  73  
  74          GP::$glossary_entry->create_and_select( $test_entries[0] );
  75          $entries = $glossary->get_entries();
  76  
  77          $this->assertCount( 1, $entries );
  78          $this->assertInstanceOf( 'GP_Glossary_Entry', $entries[0] );
  79  
  80          $this->assertEquals( $entries[0]->term, $test_entries[0]['term'] );
  81          $this->assertEquals( $entries[0]->translation, $test_entries[0]['translation'] );
  82  
  83          GP::$glossary_entry->create_and_select( $test_entries[1] );
  84  
  85          // Test that caching is working.
  86          $this->assertCount( 1, $glossary->get_entries() );
  87  
  88          $new_glossary = GP::$glossary->get( $glossary->id );
  89          $this->assertCount( 2, $new_glossary->get_entries() );
  90      }
  91  
  92      /**
  93       * @ticket gh-435
  94       */
  95  	function test_locale_glossary() {
  96          $locale = $this->factory->locale->create();
  97          $locale_set = $this->factory->translation_set->create( array( 'project_id' => 0, 'locale' => $locale->slug ) );
  98          $locale_glossary = GP::$glossary->create_and_select( array( 'translation_set_id' => $locale_set->id ) );
  99  
 100          $args = array(
 101              'locale' => $locale_set->locale,
 102              'slug' => $locale_set->slug,
 103          );
 104          $set = $this->factory->translation_set->create_with_project( $args );
 105          $glossary = GP::$glossary->create_and_select( array( 'translation_set_id' => $set->id ) );
 106  
 107          $test_entries = array(
 108              array(
 109                  'term' => 'Term',
 110                  'part_of_speech' => 'noun',
 111                  'translation' => 'Translation',
 112                  'glossary_id' => $glossary->id,
 113              ),
 114              array(
 115                  'term' => 'Term 2',
 116                  'part_of_speech' => 'noun',
 117                  'translation' => 'Translation 2',
 118                  'glossary_id' => $locale_glossary->id,
 119              ),
 120          );
 121  
 122          GP::$glossary_entry->create_and_select( $test_entries[0] );
 123          $this->assertCount( 1, $glossary->get_entries() );
 124  
 125          $route = new Testable_GP_Route_Translation;
 126          $extended_glossary = $route->testable_get_extended_glossary( $set, $set->project );
 127          $this->assertCount( 1, $extended_glossary->get_entries() );
 128  
 129          $locale_glossary_entry = GP::$glossary_entry->create_and_select( $test_entries[1] );
 130          $this->assertCount( 1, $locale_glossary->get_entries() );
 131  
 132          $route = new Testable_GP_Route_Translation;
 133          $extended_glossary = $route->testable_get_extended_glossary( $set, $set->project );
 134          $this->assertCount( 2, $extended_glossary->get_entries() );
 135  
 136          $locale_glossary_entry->term = 'Term';
 137          $locale_glossary_entry->save();
 138  
 139          $route = new Testable_GP_Route_Translation;
 140          $extended_glossary = $route->testable_get_extended_glossary( $set, $set->project );
 141          $entries = $extended_glossary->get_entries();
 142  
 143          // Count is now 1 as the term is overwritten by the project glossary.
 144          $this->assertCount( 1, $entries );
 145          $this->assertEquals( $test_entries[0]['translation'], $entries[0]->translation );
 146      }
 147  
 148      /**
 149       * @ticket gh-435
 150       */
 151  	function test_locale_glossary_without_project_glossary() {
 152          $locale = $this->factory->locale->create();
 153          $locale_set = $this->factory->translation_set->create( array( 'project_id' => 0, 'locale' => $locale->slug ) );
 154          $locale_glossary = GP::$glossary->create_and_select( array( 'translation_set_id' => $locale_set->id ) );
 155  
 156          $set = $this->factory->translation_set->create_with_project( array(
 157              'locale' => $locale_set->locale,
 158              'slug' => $locale_set->slug,
 159          ) );
 160  
 161          $entry = array(
 162              'term'           => 'Term 2',
 163              'part_of_speech' => 'noun',
 164              'translation'    => 'Translation 2',
 165              'glossary_id'    => $locale_glossary->id,
 166          );
 167  
 168          GP::$glossary_entry->create( $entry );
 169  
 170          $route = new Testable_GP_Route_Translation;
 171          $extended_glossary = $route->testable_get_extended_glossary( $set, $set->project );
 172          $this->assertInstanceOf( 'GP_Glossary', $extended_glossary );
 173  
 174          $entries = $extended_glossary->get_entries();
 175          $this->assertCount( 1, $entries );
 176          $this->assertEquals( $entry['translation'], $entries[0]->translation );
 177      }
 178  }
 179  
 180  /**
 181   * Class that makes it possible to test protected functions.
 182   */
 183  class Testable_GP_Route_Translation extends GP_Route_Translation {
 184      /**
 185       * Wraps the protected get_extended_glossary function
 186       *
 187       * @param  GP_Translation_Set $translation_set translation_set for which to retrieve the glossary.
 188       * @param  GP_Project         $project         project for finding potential parent projects.
 189       * @return GP_Glossary                         extended glossary
 190       */
 191  	public function testable_get_extended_glossary( $translation_set, $project ) {
 192          return $this->get_extended_glossary( $translation_set, $project );
 193      }
 194  }


Generated: Thu Apr 25 01:01:24 2024 Cross-referenced by PHPXref 0.7.1