comparison sites/all/modules/custom/solrconnect/tests/solr_base_query.test @ 0:015d06b10d37 default tip

initial
author dwinter
date Wed, 31 Jul 2013 13:49:13 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:015d06b10d37
1 <?php
2
3 /**
4 * Unit tests for query object methods.
5 */
6 class SolrBaseQueryTests extends DrupalUnitTestCase {
7 public static function getInfo() {
8 return array(
9 'name' => 'SolrBaseQuery Unit tests',
10 'description' => 'Unit Tests for queries.',
11 'group' => 'ApacheSolr',
12 );
13 }
14
15 function setUp() {
16 parent::setUp();
17 require_once dirname(dirname(realpath(__FILE__))) . '/apachesolr.module';
18 require_once dirname(dirname(realpath(__FILE__))) . '/apachesolr.interface.inc';
19 require_once dirname(dirname(realpath(__FILE__))) . '/Solr_Base_Query.php';
20 require_once dirname(dirname(realpath(__FILE__))) . '/Drupal_Apache_Solr_Service.php';
21 require_once dirname(dirname(realpath(__FILE__))) . '/tests/Dummy_Solr.php';
22 }
23
24 /**
25 * Helper function to simulate the auto loading and other non-needed functions
26 * that otherwise require a database
27 * @see apachesolr_drupal_query().
28 * @return SolrBaseQuery
29 */
30 private function _apachesolr_drupal_query($name, $params = array(), $solrsort = '', $base_path = '', $solr = NULL) {
31 if (empty($solr)) {
32 $solr = new DummySolr(NULL);
33 }
34 return new SolrBaseQuery($name, $solr, $params, $solrsort, $base_path);
35 }
36
37 private function _apachesolr_drupal_subquery($operator = 'OR') {
38 return new SolrFilterSubQuery($operator);
39 }
40
41 /**
42 * Test ordering of parsed filter positions.
43 *
44 * Regression test for http://drupal.org/node/891962
45 */
46 function testParseFilters() {
47 $fq = array('tid:3', 'sort_label:hello', 'tid:11', 'tid:1', 'tid:12', 'label:hello');
48 // Setup dummy Solr object.
49 $query = $this->_apachesolr_drupal_query("apachesolr_tests", array('q' => 'mykeys', 'fq' => $fq), 'sort_label asc', 'search/test');
50 // Check sortsort
51 $this->assertEqual(array('solrsort' => 'sort_label asc'), $query->getSolrsortUrlQuery());
52 $query->setSolrsort('sort_name', 'desc');
53 $this->assertEqual(array('solrsort' => 'sort_name desc'), $query->getSolrsortUrlQuery());
54 $query->setSolrsort('score', 'desc');
55 $this->assertEqual(array(), $query->getSolrsortUrlQuery());
56 // Check getPath() functionality
57 $this->assertEqual('search/test/mykeys', $query->getPath());
58 $this->assertEqual('search/test/newkeys', $query->getPath('newkeys'));
59 // Check hasFilter functionality
60 $this->assertFalse($query->hasFilter('label', 'Jello'), "hasFilter('label', 'Jello') is FALSE");
61 $this->assertTrue($query->hasFilter('label', 'hello'), "hasFilter('label', 'hello') is TRUE");
62 $this->assertTrue($query->hasFilter('label', 'hello', FALSE), "hasFilter('label', 'hello', FALSE) is TRUE");
63 $this->assertFalse($query->hasFilter('label', 'hello', TRUE), "hasFilter('label', 'hello', TRUE) is FALSE");
64 $filters = $query->getFilters();
65 $this->assertEqual(count($filters), 6, count($filters) . ' filters found, expected 6 filters');
66 // Check positions of filters
67 foreach ($fq as $idx => $filter) {
68 $this->assertEqual($filter, $query->makeFilterQuery($filters[$idx]));
69 }
70 // Check that the query string is re-assembled correctly
71 $this->assertEqual($fq, $query->getParam('fq'));
72 $this->assertEqual('mykeys', $query->getParam('q'));
73 $query->removeFilter('tid', '11');
74 $filters = $query->getFilters();
75 $this->assertEqual(count($filters), 5, count($filters) . ' filters found, expected 5 filters');
76 $this->assertEqual(array('tid:3', 'sort_label:hello', 'tid:1', 'tid:12', 'label:hello'), $query->getParam('fq'));
77 $query->removeFilter('tid');
78 $filters = $query->getFilters();
79 $this->assertEqual(count($filters), 2, count($filters) . ' filters found, expected 2 filters');
80 $this->assertEqual(array('sort_label:hello', 'label:hello'), $query->getParam('fq'));
81
82 $subquery = $this->_apachesolr_drupal_subquery();
83 $subquery->addFilter('access__all', 0);
84 $subquery->addFilter('hash', 'randomhash');
85 $query->addFilterSubQuery($subquery);
86 $this->assertEqual(count($query->getParam('fq')), 3, count($query->getParam('fq')) . ' fq params found, expected 3 after adding subquery');
87 $this->assertEqual(array('sort_label:hello', 'label:hello', '(access__all:0 OR hash:randomhash' . ')'), $query->getParam('fq'));
88 }
89
90 function testAddParams() {
91 $examples = array();
92 $examples['{!cache=false}inStock:true'] = array(
93 '#local' => 'cache=false',
94 '#exclude' => FALSE,
95 '#name' => 'inStock',
96 '#value' => 'true',
97 );
98 $examples['{!frange l=1 u=4 cache=false}sqrt(popularity)'] = array(
99 '#local' => 'frange l=1 u=4 cache=false',
100 '#exclude' => FALSE,
101 '#name' => NULL,
102 '#value' => 'sqrt(popularity)',
103 );
104 $examples['{!cache=false cost=5}inStock:true'] = array(
105 '#local' => 'cache=false cost=5',
106 '#exclude' => FALSE,
107 '#name' => 'inStock',
108 '#value' => 'true',
109 );
110 $examples['{!tag=impala}model:Impala'] = array(
111 '#local' => 'tag=impala',
112 '#exclude' => FALSE,
113 '#name' => 'model',
114 '#value' => 'Impala',
115 );
116 $examples['{!anything that appears to be local}'] = array(
117 '#local' => 'anything that appears to be local',
118 '#exclude' => FALSE,
119 '#name' => NULL,
120 '#value' => NULL,
121 );
122 $examples['bundle:(article OR page)'] = array(
123 '#local' => NULL,
124 '#exclude' => FALSE,
125 '#name' => 'bundle',
126 '#value' => '(article OR page)',
127 );
128 $examples['-bundle:(article OR page)'] = array(
129 '#local' => NULL,
130 '#exclude' => TRUE,
131 '#name' => 'bundle',
132 '#value' => '(article OR page)',
133 );
134 $examples['-{!anything that appears to be local}'] = array(
135 '#local' => 'anything that appears to be local',
136 '#exclude' => TRUE,
137 '#name' => NULL,
138 '#value' => NULL,
139 );
140 $examples['title:"double words"'] = array(
141 '#local' => NULL,
142 '#exclude' => FALSE,
143 '#name' => 'title',
144 '#value' => '"double words"',
145 );
146 $examples['field_date:[1970-12-31T23:59:59Z TO NOW]'] = array(
147 '#local' => NULL,
148 '#exclude' => FALSE,
149 '#name' => 'field_date',
150 '#value' => '[1970-12-31T23:59:59Z TO NOW]',
151 );
152
153 $query = $this->_apachesolr_drupal_query("apachesolr_tests");
154 foreach ($examples as $fq => $example) {
155 $name = (!empty($example['#name'])) ? $example['#name'] : '_QUERY_';
156 $value = (!empty($example['#value'])) ? $example['#value'] : '_VALUE_';
157 $filter = $name . ':' . $value;
158 // Check if filter is seen as a valid one
159 $message = t('Filter (@fq) is Valid', array('@fq' => $filter));
160 $this->assertTrue($query->validFilterValue($filter), $message);
161
162 $query->addParam('fq', $fq);
163 // Check if filter was added
164 $message = t('hasFilter(@name, @value) is True', array('@name' => $example['#name'], '@value' => $example['#value']));
165 $this->assertTrue($query->hasFilter($example['#name'], $example['#value'], $example['#exclude']), $message);
166 $filters = $query->getFilters();
167 $filter = reset($filters);
168 $message = t('The filter "@fq" was added with all the given properties', array('@fq' => $fq));
169 $this->assertTrue(!array_diff($example, $filter), $message);
170 $query->removeFilter($example['#name']);
171 $message = t('The filter "@fq" was correctly removed', array('@fq' => $fq));
172 $this->assertFalse($query->hasFilter($example['#name'], $example['#value'], $example['#exclude']), $message);
173 // Since we cannot remove filters without names yet we have to clear the whole fq array
174 $query->removeParam('fq');
175 // Check the ones without the name also
176 }
177
178 // Check for invalid combinations
179 $bad_examples['wrong name:"double words"'] = array(
180 '#local' => NULL,
181 '#exclude' => FALSE,
182 '#name' => 'wrong name',
183 '#value' => '"double words"',
184 );
185 $bad_examples['field_date:[1970-12-31 TO NOW]'] = array(
186 '#local' => NULL,
187 '#exclude' => FALSE,
188 '#name' => 'field_date',
189 '#value' => '[1970-12-31 TO NOW]',
190 );
191 $bad_examples['bundle:((article OR page)]'] = array(
192 '#local' => NULL,
193 '#exclude' => FALSE,
194 '#name' => 'bundle',
195 '#value' => '((article OR page)]',
196 );
197
198 foreach ($bad_examples as $fq => $example) {
199 $name = (!empty($example['#name'])) ? $example['#name'] : '_QUERY_';
200 $value = (!empty($example['#value'])) ? $example['#value'] : '_VALUE_';
201 $filter = $name . ':' . $value;
202 // Check if filter is seen as a valid one
203 $message = t('Filter (@fq) is not Valid', array('@fq' => $filter));
204 $this->assertFalse($query->validFilterValue($filter), $message);
205 }
206 // Check parameter normalization.
207 $query->addParam('spellcheck', TRUE);
208 $this->assertTrue($query->getParam('spellcheck') === 'true', "TRUE normalized to string 'true'");
209 $query->replaceParam('spellcheck', FALSE);
210 $this->assertTrue($query->getParam('spellcheck') === 'false', "FALSE normalized to string 'false'");
211 $query->addParam('fl', array(' x ', TRUE));
212 $this->assertTrue($query->getParam('fl') === array('x', 'true'), "Array of params all normalized");
213 }
214 }