Source of file TranslatableUnitTest.php
Size: 9,091 Bytes - Last Modified: 2021-12-23T10:58:34+00:00
/var/www/docs.ssmods.com/process/src/tests/long/TranslatableUnitTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 | <?php use SilverStripe\Elastica\ElasticaSearcher; use SilverStripe\Elastica\ReindexTask; class TranslatableUnitTest extends ElasticsearchTestBase { public static $fixture_file = 'elastica/tests/lotsOfPhotos.yml'; public static $ignoreFixtureFileFor = array('testResultsForEmptySearch'); public function setUpOnce() { //Add translatable if it exists if (class_exists('Translatable')) { SiteTree::add_extension('Translatable'); } parent::setUpOnce(); } public function testElasticSearchForm() { if (!class_exists('Translatable')) { $this->markTestSkipped('Translatable not installed'); } $form = new \ElasticSearchForm(new \Controller(), 'TestForm'); $fields = $form->Fields(); $result = array(); foreach ($fields as $field) { $result[$field->getName()] = $field->Value(); } $expected = array('q' => '', 'searchlocale' => 'en_US'); $this->assertEquals($expected, $result); } public function testHighlightPassingFields() { if (!class_exists('Translatable')) { $this->markTestSkipped('Translatable not installed'); } $es = new ElasticaSearcher(); $es->setClasses('FlickrPhotoTO'); $es->setHighlightedFields(array('Title', 'Title.standard', 'Description')); $fields = array('Title' => 1, 'Description' => 1); $query = 'New Zealand'; $paginated = $es->search($query, $fields); $ctr = 0; foreach ($paginated->getList()->toArray() as $result) { ++$ctr; foreach ($result->SearchHighlightsByField->Description->getIterator() as $highlight) { $snippet = $highlight->Snippet; $snippet = strtolower($snippet); $wordFound = false; $lcquery = explode(' ', strtolower($query)); foreach ($lcquery as $part) { $bracketed = '<strong class="hl">'.$part.'</strong>'; if (strpos($snippet, $bracketed) > 0) { $wordFound = true; } } $this->assertTrue($wordFound, 'Highlight should have been found'); } } } public function testAutoCompleteGood() { if (!class_exists('Translatable')) { $this->markTestSkipped('Translatable not installed'); } $es = new ElasticaSearcher(); $es->setClasses('FlickrPhotoTO'); $query = 'Lond'; $results = $es->autocomplete_search($query, 'Title'); $this->assertEquals(7, $results->getTotalItems()); foreach ($results->toArray() as $result) { $this->assertTrue(strpos($result->Title, $query) > 0); } } // --------------------- // FIXME - this test is shardy unfortunately public function testMoreLikeThisSinglePhoto() { if (!class_exists('Translatable')) { $this->markTestSkipped('Translatable not installed'); } $fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); $es = new ElasticaSearcher(); $locale = \i18n::default_locale(); $es->setLocale($locale); $es->setClasses('FlickrPhotoTO'); $fields = array('Description.standard' => 1, 'Title.standard' => 1); $results = $es->moreLikeThis($fp, $fields, true); $terms = $results->getList()->MoreLikeThisTerms; $fieldNamesReturned = array_keys($terms); $fieldNames = array_keys($fields); sort($fieldNames); sort($fieldNamesReturned); $this->assertEquals($fieldNames, $fieldNamesReturned); //FIXME - this seems anomolyous, check in more detail $expected = array('texas'); $this->assertEquals($expected, $terms['Title.standard']); $expected = array('collection', 'company', 'degolyer', 'everett', 'file', 'high', 'information', 'new', 'orleans', 'pacific', 'photographs', 'railroad', 'resolution', 'see', 'southern', 'texas', 'view', ); $actual = $terms['Description.standard']; sort($expected); sort($actual); $this->assertEquals($expected, $actual); } /* test blank fields test fields with no weighting (ie not associative) */ public function testSimilarGood() { if (!class_exists('Translatable')) { $this->markTestSkipped('Translatable not installed'); } $fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); $es = new ElasticaSearcher(); $es->setClasses('FlickrPhotoTO'); $fields = array('Title.standard' => 1, 'Description.standard' => 1); $paginated = $es->moreLikeThis($fp, $fields, true); $this->assertEquals(32, $paginated->getTotalItems()); $results = $paginated->getList()->toArray(); $this->assertEquals('[Texas and New Orleans, Southern Pacific Railroad Station, Stockdale, Texas]', $results[0]->Title); $this->assertEquals('[Texas and New Orleans, Southern Pacific Railroad Station, Taft, Texas]', $results[1]->Title); $this->assertEquals('[Texas and New Orleans, Southern Pacific Railroad Station, Sierra Blanca, Texas]', $results[2]->Title); $this->assertEquals('[Texas and New Orleans, Southern Pacific Freight Station, Waxahachie, Texas]', $results[3]->Title); $this->assertEquals('[Texas and New Orleans, Southern Pacific Passenger Station, Waxahachie, Texas]', $results[4]->Title); $this->assertEquals('[Texas and New Orleans, Southern Pacific, Tower No. 63, Mexia, Texas]', $results[5]->Title); $this->assertEquals('[Texas and New Orleans, Southern Pacific, Eakin Street Yard Office, Dallas, Texas]', $results[6]->Title); $this->assertEquals('[Texas and New Orleans, Southern Pacific Locomotive Scrap Line, Englewood Yards, Houston, Texas]', $results[7]->Title); $this->assertEquals("[Texas and New Orleans, Southern Pacific, Switchman's Tower, San Antonio, Texas]", $results[8]->Title); $this->assertEquals('Flash Light view in new Subterranean', $results[9]->Title); } /* FIXME - this is not working, not sure why. Trying to complete coverage of ReindexTask */ public function testBulkIndexing() { if (!class_exists('Translatable')) { $this->markTestSkipped('Translatable not installed'); } //Reset the index, so that nothing has been indexed $this->service->reset(); //Number of requests indexing wise made to Elasticsearch server $reqs = $this->service->getIndexingRequestCtr(); $task = new ReindexTask($this->service); // null request is fine as no parameters used $task->run(null); //Check that the number of indexing requests has increased by 2 $deltaReqs = $this->service->getIndexingRequestCtr() - $reqs; //One call is made for each of Page and FlickrPhotoTO $this->assertEquals(2, $deltaReqs); // default installed pages plus 100 FlickrPhotoTOs $this->checkNumberOfIndexedDocuments(103); } // if this is not set to unbounded, zero, a conditional is triggered to add max doc freq to the request public function testSimilarChangeMaxDocFreq() { if (!class_exists('Translatable')) { $this->markTestSkipped('Translatable not installed'); } $fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); $es = new ElasticaSearcher(); $es->setMaxDocFreq(4); $es->setClasses('FlickrPhotoTO'); $fields = array('Title.standard' => 1, 'Description.standard' => 1); $paginated = $es->moreLikeThis($fp, $fields, true); $this->assertEquals(14, $paginated->getTotalItems()); } public function testSimilarNullFields() { if (!class_exists('Translatable')) { $this->markTestSkipped('Translatable not installed'); } $fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); $es = new ElasticaSearcher(); $es->setClasses('FlickrPhotoTO'); try { $es->moreLikeThis($fp, null, true); $this->fail('More like this search should have failed'); } catch (InvalidArgumentException $e) { $this->assertEquals('Fields cannot be null', $e->getMessage()); } } public function testSimilarNullItem() { if (!class_exists('Translatable')) { $this->markTestSkipped('Translatable not installed'); } $es = new ElasticaSearcher(); $es->setClasses('FlickrPhotoTO'); $fields = array('Title.standard' => 1, 'Description.standard' => 1); try { $es->moreLikeThis(null, $fields, true); $this->fail('Search should have failed'); } catch (InvalidArgumentException $e) { $this->assertEquals('A searchable item cannot be null', $e->getMessage()); } } } |