1<?php 2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project 3// 4// All Rights Reserved. See copyright.txt for details and a complete list of authors. 5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details. 6// $Id$ 7 8class Search_Elastic_FacetTest extends PHPUnit_Framework_TestCase 9{ 10 function setUp() 11 { 12 $elasticSearchHost = empty(getenv('ELASTICSEARCH_HOST')) ? 'localhost' : getenv('ELASTICSEARCH_HOST'); 13 $connection = new Search_Elastic_Connection('http://' . $elasticSearchHost . ':9200'); 14 15 $status = $connection->getStatus(); 16 if (! $status->ok) { 17 $this->markTestSkipped('Elasticsearch needs to be available on ' . $elasticSearchHost . ':9200 for the test to run.'); 18 } 19 20 $this->index = new Search_Elastic_Index($connection, 'test_index'); 21 $this->index->destroy(); 22 23 $this->populate($this->index); 24 } 25 26 function tearDown() 27 { 28 if ($this->index) { 29 $this->index->destroy(); 30 } 31 } 32 33 function testRequireFacet() 34 { 35 $facet = new Search_Query_Facet_Term('categories'); 36 37 $query = new Search_Query; 38 $query->filterType('wiki page'); 39 $query->requestFacet($facet); 40 41 $result = $query->search($this->index); 42 $values = $result->getFacet($facet); 43 44 $this->assertEquals( 45 new Search_ResultSet_FacetFilter( 46 $facet, 47 [ 48 ['value' => 1, 'count' => 3], 49 ['value' => 2, 'count' => 2], 50 ['value' => 3, 'count' => 1], 51 ['value' => 'orphan', 'count' => 1], 52 ] 53 ), 54 $values 55 ); 56 } 57 58 protected function populate($index) 59 { 60 $this->add($index, 'ABC', [1, 2, 3]); 61 $this->add($index, 'AB', [1, 2]); 62 $this->add($index, 'A', [1]); 63 $this->add($index, 'empty', ['orphan']); 64 } 65 66 private function add($index, $page, array $categories) 67 { 68 $typeFactory = $index->getTypeFactory(); 69 70 $index->addDocument( 71 [ 72 'object_type' => $typeFactory->identifier('wiki page'), 73 'object_id' => $typeFactory->identifier($page), 74 'categories' => $typeFactory->multivalue($categories), 75 ] 76 ); 77 } 78} 79