Source of file SolrIndexingTest.php
Size: 7,925 Bytes - Last Modified: 2021-12-23T10:34:35+00:00
/var/www/docs.ssmods.com/process/src/tests/SolrIndexingTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 | <?php /** Copyright (c) 2009, SilverStripe Australia PTY LTD - www.silverstripe.com.au All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of SilverStripe nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ class SolrIndexingTest extends SapphireTest { static $fixture_file = 'solr/tests/SolrTest.yml'; public function setUpOnce() { DataObject::add_extension('SiteTree', 'SolrIndexable'); parent::setUpOnce(); } public function tearDownOnce() { DataObject::remove_extension('SiteTree', 'SolrIndexable'); parent::tearDownOnce(); } public function testSolrIndexableItem() { $item = $this->objFromFixture('Page','page1'); $indexFields = array( 'Title' => array( 'Type' => 'Varchar', 'Value' => 'Page 1' ), 'Content' => array( 'Type' => 'HTMLText', 'Value' => 'Text content in the page', ), 'ID' => 1, 'ClassName' => 'Page' ); // call onAfterPublish, we expect the $searchService = $this->getMock('SolrSearchService', array('index')); $searchService->expects($this->once()) ->method('index'); // ->with($this->equalTo($indexFields)); Injector::inst()->registerService($searchService, 'SolrSearchService'); // now save the item and hope it gets indexed $item->extend('onAfterPublish', $item); } public function testIndexObject() { $search = new SolrSearchService; $converted = $search->convertObjectToDocument(array( 'ID' => 1, 'Title' => 'This document', 'Category' => 'Docs', 'Tags' => array('green', 'food') )); } public function testStoreIndexAndQuery() { Injector::inst()->registerService(new SolrSearchService()); // we should be able to perform a query for documents with 'text content' // and receive back some valid data $search = singleton('SolrSearchService'); if (!$search->isConnected()) { return; } // clear everything out, then index content /* @var $search SolrSearchService */ $search->getSolr()->deleteByQuery('*:*'); $item = $this->objFromFixture('Page','page1'); $item->extend('onAfterPublish', $item); $results = $search->query('text:"Text Content"'); $results = $results->getResult()->response; $this->assertNotNull($results->docs); $this->assertEquals(1, count($results->docs)); $this->assertEquals('Page_1_Live', $results->docs[0]->id); } public function testQueryForObjects() { Injector::inst()->registerService(new SolrSearchService()); // we should be able to perform a query for documents with 'text content' // and receive back some valid data $search = singleton('SolrSearchService'); if (!$search->isConnected()) { return; } // clear everything out, then index content /* @var $search SolrSearchService */ $search->getSolr()->deleteByQuery('*:*'); $item = $this->objFromFixture('Page','page1'); $item->extend('onAfterPublish', $item); $results = $search->query('text:"Text Content"'); $results = $results->getDataObjects(); $this->assertTrue($results instanceof SS_List); $this->assertEquals(1, $results->count()); $item = $results->First(); $this->assertEquals('Page', $item->ClassName); $this->assertEquals(1, $item->ID); $search->unindex($item); $results = $search->query('text:"Text Content"'); $results = $results->getDataObjects(); $this->assertTrue($results instanceof SS_List); $this->assertEquals(0, $results->count()); } public function testFacetQuery() { Injector::inst()->registerService(new SolrSearchService()); // we should be able to perform a query for documents with 'text content' // and receive back some valid data $search = singleton('SolrSearchService'); if (!$search->isConnected()) { return; } // clear everything out, then index content /* @var $search SolrSearchService */ $search->getSolr()->deleteByQuery('*:*'); $item = $this->objFromFixture('Page','page1'); $item->extend('onAfterPublish', $item); $item = $this->objFromFixture('Page','page2'); $item->extend('onAfterPublish', $item); $item = $this->objFromFixture('Page','page3'); $item->extend('onAfterPublish', $item); $item = $this->objFromFixture('Page','page4'); $item->extend('onAfterPublish', $item); $results = $search->getFacetsForFields('text'); $facets = $results->getFacets(); $this->assertNotNull($facets); $this->assertEquals(1, count($facets)); $this->assertTrue(isset($facets['text'])); $this->assertEquals(4, count($facets['text'])); } public function testMultipleFacetQuery() { Injector::inst()->registerService(new SolrSearchService()); // we should be able to perform a query for documents with 'text content' // and receive back some valid data $search = singleton('SolrSearchService'); if (!$search->isConnected()) { return; } // clear everything out, then index content /* @var $search SolrSearchService */ $search->getSolr()->deleteByQuery('*:*'); $item = $this->objFromFixture('Page','page1'); $item->extend('onAfterPublish', $item); $item = $this->objFromFixture('Page','page2'); $item->extend('onAfterPublish', $item); $item = $this->objFromFixture('Page','page3'); $item->extend('onAfterPublish', $item); $item = $this->objFromFixture('Page','page4'); $item->extend('onAfterPublish', $item); $results = $search->getFacetsForFields(array('text', 'title')); $facets = $results->getFacets(); $this->assertNotNull($facets); $this->assertEquals(2, count($facets)); $this->assertTrue(isset($facets['title'])); $this->assertTrue(isset($facets['text'])); $this->assertEquals(4, count($facets['title'])); $this->assertEquals(4, count($facets['text'])); } public function testStageQuerying() { Injector::inst()->registerService(new SolrSearchService()); // we should be able to perform a query for documents with 'text content' // and receive back some valid data $search = singleton('SolrSearchService'); if (!$search->isConnected()) { return; } // clear everything out, then index content /* @var $search SolrSearchService */ $search->getSolr()->deleteByQuery('*:*'); $item = $this->objFromFixture('Page','home'); $item->write(); $item->doPublish(); $results = $search->query('title:home'); $objects = $results->getDataObjects(); // there should actually be two results; one for each stage. $this->assertEquals(2, $objects->count()); // now set a stage Versioned::choose_site_stage(); $results = $search->query('title:home'); $objects = $results->getDataObjects(); $this->assertEquals(1, $objects->count()); } } |