This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | use SilverStripe\Elastica\ElasticSearcher; |
||
3 | use Elastica\Type; |
||
4 | class ElasticSearcherUnitTest extends ElasticsearchBaseTest { |
||
5 | public static $fixture_file = 'elastica/tests/lotsOfPhotos.yml'; |
||
6 | |||
7 | public static $ignoreFixtureFileFor = array('testResultsForEmptySearch'); |
||
8 | |||
9 | public function setUp() { |
||
10 | parent::setUp(); |
||
11 | } |
||
12 | |||
13 | public function tearDown() { |
||
14 | parent::tearDown(); |
||
15 | } |
||
16 | |||
17 | |||
18 | public function testSuggested() { |
||
19 | $es = new ElasticSearcher(); |
||
20 | $locale = \i18n::default_locale(); |
||
21 | $es->setLocale($locale); |
||
22 | $es->setClasses('FlickrPhotoTO'); |
||
23 | $fields = array('Title' => 1, 'Description' => 1); |
||
24 | $results = $es->search('New Zealind', $fields, true); |
||
25 | $this->assertEquals(100, $results->getTotalItems()); |
||
26 | $this->assertEquals('New Zealand', $es->getSuggestedQuery()); |
||
27 | } |
||
28 | |||
29 | |||
30 | public function testResultsForEmptySearch() { |
||
31 | $es = new ElasticSearcher(); |
||
32 | |||
33 | $es->hideResultsForEmptySearch(); |
||
34 | $this->assertFalse($es->getShowResultsForEmptySearch()); |
||
35 | |||
36 | $es->showResultsForEmptySearch(); |
||
37 | $this->assertTrue($es->getShowResultsForEmptySearch()); |
||
38 | } |
||
39 | |||
40 | |||
41 | public function testMoreLikeThisSinglePhoto() { |
||
42 | $fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); |
||
43 | $es = new ElasticSearcher(); |
||
44 | $locale = \i18n::default_locale(); |
||
45 | $es->setLocale($locale); |
||
46 | $es->setClasses('FlickrPhotoTO'); |
||
47 | |||
48 | $fields = array('Description.standard' => 1, 'Title.standard' => 1); |
||
49 | $results = $es->moreLikeThis($fp, $fields, true); |
||
0 ignored issues
–
show
|
|||
50 | |||
51 | $terms = $results->getList()->MoreLikeThisTerms; |
||
0 ignored issues
–
show
Accessing
MoreLikeThisTerms on the interface SS_List suggest that you code against a concrete implementation. How about adding an instanceof check?
If you access a property on an interface, you most likely code against a concrete implementation of the interface. Available Fixes
![]() |
|||
52 | |||
53 | $fieldNamesReturned = array_keys($terms); |
||
54 | $fieldNames = array_keys($fields); |
||
55 | sort($fieldNames); |
||
56 | sort($fieldNamesReturned); |
||
57 | |||
58 | $this->assertEquals($fieldNames, $fieldNamesReturned); |
||
59 | |||
60 | //FIXME - this seems anomolyous, check in more detail |
||
61 | $expected = array('texas'); |
||
62 | $this->assertEquals($expected, $terms['Title.standard']); |
||
63 | |||
64 | $expected = array('collection', 'company', 'degolyer', 'everett', 'file', 'high', |
||
65 | 'information', 'new', 'orleans', 'pacific', 'photographs', 'railroad', 'resolution', |
||
66 | 'see', 'southern', 'texas', 'view'); |
||
67 | |||
68 | |||
69 | |||
70 | $actual = $terms['Description.standard']; |
||
71 | sort($expected); |
||
72 | sort($actual); |
||
73 | |||
74 | |||
75 | $this->assertEquals($expected, $actual); |
||
76 | } |
||
77 | |||
78 | |||
79 | |||
80 | public function testSimilarNoWeighting() { |
||
81 | $fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); |
||
82 | $es = new ElasticSearcher(); |
||
83 | $es->setClasses('FlickrPhotoTO'); |
||
84 | $fields = array('Title.standard', 'Description.standard'); |
||
85 | try { |
||
86 | $paginated = $es->moreLikeThis($fp, $fields, true); |
||
0 ignored issues
–
show
$paginated is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() It seems like
$fp defined by $this->objFromFixture('F...rPhotoTO', 'photo0076') on line 81 can be null ; however, SilverStripe\Elastica\El...earcher::moreLikeThis() does not accept null , maybe add an additional type check?
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: /** @return stdClass|null */
function mayReturnNull() { }
function doesNotAcceptNull(stdClass $x) { }
// With potential error.
function withoutCheck() {
$x = mayReturnNull();
doesNotAcceptNull($x); // Potential error here.
}
// Safe - Alternative 1
function withCheck1() {
$x = mayReturnNull();
if ( ! $x instanceof stdClass) {
throw new \LogicException('$x must be defined.');
}
doesNotAcceptNull($x);
}
// Safe - Alternative 2
function withCheck2() {
$x = mayReturnNull();
if ($x instanceof stdClass) {
doesNotAcceptNull($x);
}
}
![]() |
|||
87 | $this->fail('Query has no weight and thus should have failed'); |
||
88 | } catch (InvalidArgumentException $e) { |
||
89 | $this->assertEquals('Fields must be of the form fieldname => weight', $e->getMessage()); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | |||
94 | public function testSimilarWeightingNotNumeric() { |
||
95 | $fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); |
||
96 | $es = new ElasticSearcher(); |
||
97 | $es->setClasses('FlickrPhotoTO'); |
||
98 | $fields = array('Title.standard' => 4, 'Description.standard' => 'not numeric'); |
||
99 | try { |
||
100 | $paginated = $es->moreLikeThis($fp, $fields, true); |
||
0 ignored issues
–
show
$paginated is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() It seems like
$fp defined by $this->objFromFixture('F...rPhotoTO', 'photo0076') on line 95 can be null ; however, SilverStripe\Elastica\El...earcher::moreLikeThis() does not accept null , maybe add an additional type check?
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: /** @return stdClass|null */
function mayReturnNull() { }
function doesNotAcceptNull(stdClass $x) { }
// With potential error.
function withoutCheck() {
$x = mayReturnNull();
doesNotAcceptNull($x); // Potential error here.
}
// Safe - Alternative 1
function withCheck1() {
$x = mayReturnNull();
if ( ! $x instanceof stdClass) {
throw new \LogicException('$x must be defined.');
}
doesNotAcceptNull($x);
}
// Safe - Alternative 2
function withCheck2() {
$x = mayReturnNull();
if ($x instanceof stdClass) {
doesNotAcceptNull($x);
}
}
![]() |
|||
101 | $this->fail('Query has non numeric weight and thus should have failed'); |
||
102 | } catch (InvalidArgumentException $e) { |
||
103 | $this->assertEquals('Fields must be of the form fieldname => weight', $e->getMessage()); |
||
104 | } |
||
105 | } |
||
106 | |||
107 | |||
108 | public function testSimilarToNonSearchable() { |
||
109 | $m = Member::get()->first(); // this is not by default Searchable |
||
0 ignored issues
–
show
|
|||
110 | $es = new ElasticSearcher(); |
||
111 | $es->setClasses('FlickrPhotoTO'); |
||
112 | $fields = array('Title.standard' => 4, 'Description.standard' => 2); |
||
113 | try { |
||
114 | $paginated = $es->moreLikeThis($m, $fields, true); |
||
0 ignored issues
–
show
$paginated is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() It seems like
$m defined by \Member::get()->first() on line 109 can be null ; however, SilverStripe\Elastica\El...earcher::moreLikeThis() does not accept null , maybe add an additional type check?
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: /** @return stdClass|null */
function mayReturnNull() { }
function doesNotAcceptNull(stdClass $x) { }
// With potential error.
function withoutCheck() {
$x = mayReturnNull();
doesNotAcceptNull($x); // Potential error here.
}
// Safe - Alternative 1
function withCheck1() {
$x = mayReturnNull();
if ( ! $x instanceof stdClass) {
throw new \LogicException('$x must be defined.');
}
doesNotAcceptNull($x);
}
// Safe - Alternative 2
function withCheck2() {
$x = mayReturnNull();
if ($x instanceof stdClass) {
doesNotAcceptNull($x);
}
}
![]() |
|||
115 | $this->fail('Querying for a non searchable object, thus should have failed'); |
||
116 | } catch (InvalidArgumentException $e) { |
||
117 | $this->assertEquals('Objects of class Member are not searchable', $e->getMessage()); |
||
118 | } |
||
119 | } |
||
120 | |||
121 | public function testSimilarGood() { |
||
122 | $fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); |
||
123 | $es = new ElasticSearcher(); |
||
124 | $es->setClasses('FlickrPhotoTO'); |
||
125 | $fields = array('Title.standard' => 1, 'Description.standard' => 1); |
||
126 | $paginated = $es->moreLikeThis($fp, $fields, true); |
||
0 ignored issues
–
show
It seems like
$fp defined by $this->objFromFixture('F...rPhotoTO', 'photo0076') on line 122 can be null ; however, SilverStripe\Elastica\El...earcher::moreLikeThis() does not accept null , maybe add an additional type check?
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: /** @return stdClass|null */
function mayReturnNull() { }
function doesNotAcceptNull(stdClass $x) { }
// With potential error.
function withoutCheck() {
$x = mayReturnNull();
doesNotAcceptNull($x); // Potential error here.
}
// Safe - Alternative 1
function withCheck1() {
$x = mayReturnNull();
if ( ! $x instanceof stdClass) {
throw new \LogicException('$x must be defined.');
}
doesNotAcceptNull($x);
}
// Safe - Alternative 2
function withCheck2() {
$x = mayReturnNull();
if ($x instanceof stdClass) {
doesNotAcceptNull($x);
}
}
![]() |
|||
127 | |||
128 | $results = $paginated->getList()->toArray(); |
||
129 | |||
130 | // FIXME - this test appears fragile due to sharding issues with more like this |
||
131 | $ctr = 0; |
||
132 | if($ctr < 9) { |
||
133 | $this->assertStringStartsWith( |
||
0 ignored issues
–
show
The method
assertStringStartsWith() does not seem to exist on object<ElasticSearcherUnitTest> .
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||
134 | '[Texas and New Orleans, Southern Pacific', |
||
135 | $results[$ctr]->Title |
||
136 | ); |
||
137 | $ctr++; |
||
138 | } |
||
139 | } |
||
140 | |||
141 | |||
142 | // if this is not set to unbounded, zero, a conditional is triggered to add max doc freq to the request |
||
143 | /** |
||
144 | public function testSimilarChangeMaxDocFreq() { |
||
145 | $fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); |
||
146 | $es = new ElasticSearcher(); |
||
147 | $es->setMaxDocFreq(8); |
||
148 | $es->setClasses('FlickrPhotoTO'); |
||
149 | $fields = array('Title.standard' => 1, 'Description.standard' => 1); |
||
150 | $paginated = $es->moreLikeThis($fp, $fields, true); |
||
151 | $results = $paginated->getList()->toArray(); |
||
152 | |||
153 | foreach ($results as $result) { |
||
154 | error_log($result->Title); |
||
155 | } |
||
156 | |||
157 | $ctr = 0; |
||
158 | foreach ($results as $result) { |
||
159 | $ctr++; |
||
160 | if ($ctr < 9) { |
||
161 | $this->assertStringStartsWith( |
||
162 | '[Texas and New Orleans, Southern Pacific', |
||
163 | $result->Title |
||
164 | ); |
||
165 | } |
||
166 | |||
167 | } |
||
168 | $this->assertEquals(14, $paginated->getTotalItems()); |
||
169 | $this->makeCode($paginated); |
||
170 | } |
||
171 | **/ |
||
172 | |||
173 | public function testSimilarNullFields() { |
||
174 | $fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); |
||
175 | $es = new ElasticSearcher(); |
||
176 | $es->setClasses('FlickrPhotoTO'); |
||
177 | try { |
||
178 | $paginated = $es->moreLikeThis($fp, null, true); |
||
0 ignored issues
–
show
null is of type null , but the function expects a array .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
![]() $paginated is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() It seems like
$fp defined by $this->objFromFixture('F...rPhotoTO', 'photo0076') on line 174 can be null ; however, SilverStripe\Elastica\El...earcher::moreLikeThis() does not accept null , maybe add an additional type check?
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: /** @return stdClass|null */
function mayReturnNull() { }
function doesNotAcceptNull(stdClass $x) { }
// With potential error.
function withoutCheck() {
$x = mayReturnNull();
doesNotAcceptNull($x); // Potential error here.
}
// Safe - Alternative 1
function withCheck1() {
$x = mayReturnNull();
if ( ! $x instanceof stdClass) {
throw new \LogicException('$x must be defined.');
}
doesNotAcceptNull($x);
}
// Safe - Alternative 2
function withCheck2() {
$x = mayReturnNull();
if ($x instanceof stdClass) {
doesNotAcceptNull($x);
}
}
![]() |
|||
179 | } catch (InvalidArgumentException $e) { |
||
180 | $this->assertEquals('Fields cannot be null', $e->getMessage()); |
||
181 | } |
||
182 | } |
||
183 | |||
184 | |||
185 | public function testSimilarNullItem() { |
||
186 | $es = new ElasticSearcher(); |
||
187 | $es->setClasses('FlickrPhotoTO'); |
||
188 | $fields = array('Title.standard' => 1, 'Description.standard' => 1); |
||
189 | |||
190 | try { |
||
191 | $paginated = $es->moreLikeThis(null, $fields, true); |
||
0 ignored issues
–
show
$paginated is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() |
|||
192 | } catch (InvalidArgumentException $e) { |
||
193 | $this->assertEquals('A searchable item cannot be null', $e->getMessage()); |
||
194 | } |
||
195 | } |
||
196 | |||
197 | |||
198 | |||
199 | public function testHighlightsAsIfCMSEdited() { |
||
200 | $es = new ElasticSearcher(); |
||
201 | $locale = \i18n::default_locale(); |
||
202 | $es->setLocale($locale); |
||
203 | $es->setClasses('FlickrPhotoTO'); |
||
204 | |||
205 | $filter = array('ClazzName' => 'FlickrPhotoTO', 'Name' => 'Title'); |
||
206 | $titleField = SearchableField::get()->filter($filter)->first(); |
||
207 | $titleField->ShowHighlights = true; |
||
208 | $titleField->write(); |
||
209 | |||
210 | $filter = array('ClazzName' => 'FlickrPhotoTO', 'Name' => 'Description'); |
||
211 | $nameField = SearchableField::get()->filter($filter)->first(); |
||
212 | $nameField->ShowHighlights = true; |
||
213 | $nameField->write(); |
||
214 | |||
215 | $fields = array('Title' => 1, 'Description' => 1); |
||
216 | $query = 'New Zealand'; |
||
217 | $paginated = $es->search($query, $fields); |
||
218 | $ctr = 0; |
||
219 | |||
220 | foreach($paginated->getList()->toArray() as $result) { |
||
221 | $ctr++; |
||
222 | foreach($result->SearchHighlightsByField->Description_standard->getIterator() as $highlight) { |
||
223 | $snippet = $highlight->Snippet; |
||
224 | $snippet = strtolower($snippet); |
||
225 | $wordFound = false; |
||
226 | $lcquery = explode(' ', strtolower($query)); |
||
227 | foreach($lcquery as $part) { |
||
228 | $bracketed = '<strong class="hl">' . $part . '</strong>'; |
||
229 | if(strpos($snippet, $bracketed) > 0) { |
||
230 | $wordFound = true; |
||
231 | } |
||
232 | } |
||
233 | $this->assertTrue($wordFound, 'Highlight should have been found'); |
||
234 | } |
||
235 | } |
||
236 | } |
||
237 | |||
238 | |||
239 | public function testHighlightPassingFields() { |
||
240 | $es = new ElasticSearcher(); |
||
241 | $es->setClasses('FlickrPhotoTO'); |
||
242 | $es->setHighlightedFields(array('Title', 'Title.standard', 'Description')); |
||
243 | |||
244 | $fields = array('Title' => 1, 'Description' => 1); |
||
245 | $query = 'New Zealand'; |
||
246 | $paginated = $es->search($query, $fields); |
||
247 | $ctr = 0; |
||
248 | |||
249 | foreach($paginated->getList()->toArray() as $result) { |
||
250 | $ctr++; |
||
251 | |||
252 | foreach($result->SearchHighlightsByField->Description->getIterator() as $highlight) { |
||
253 | $snippet = $highlight->Snippet; |
||
254 | $snippet = strtolower($snippet); |
||
255 | $wordFound = false; |
||
256 | $lcquery = explode(' ', strtolower($query)); |
||
257 | foreach($lcquery as $part) { |
||
258 | $bracketed = '<strong class="hl">' . $part . '</strong>'; |
||
259 | if(strpos($snippet, $bracketed) > 0) { |
||
260 | $wordFound = true; |
||
261 | } |
||
262 | } |
||
263 | $this->assertTrue($wordFound, 'Highlight should have been found'); |
||
264 | } |
||
265 | } |
||
266 | } |
||
267 | |||
268 | |||
269 | public function testAutoCompleteGood() { |
||
270 | $es = new ElasticSearcher(); |
||
271 | $es->setClasses('FlickrPhotoTO'); |
||
272 | $fields = array('Title' => 1, 'Description' => 1); |
||
0 ignored issues
–
show
$fields is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() |
|||
273 | $query = 'Lond'; |
||
274 | $results = $es->autocomplete_search($query, 'Title'); |
||
275 | $this->assertEquals(7, $results->getTotalItems()); |
||
276 | foreach($results->toArray() as $result) { |
||
277 | $this->assertTrue(strpos($result->Title, $query) > 0); |
||
278 | } |
||
279 | } |
||
280 | |||
281 | |||
282 | private function makeCode($paginated) { |
||
283 | $results = $paginated->getList()->toArray(); |
||
284 | $ctr = 0; |
||
285 | echo '$result = $paginated->getList()->toArray();' . "\n"; |
||
286 | foreach($results as $result) { |
||
287 | echo '$this->assertEquals("' . $result->Title . '", $results[' . $ctr . ']->Title);' . "\n"; |
||
288 | $ctr++; |
||
289 | } |
||
290 | } |
||
291 | |||
292 | } |
||
293 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: