Issues (248)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

tests/ElasticaServiceTest.php (8 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
use SilverStripe\Elastica\ElasticaService;
4
use SilverStripe\Elastica\ReindexTask;
5
use SilverStripe\Elastica\DeleteIndexTask;
6
use SilverStripe\Elastica\Searchable;
7
8
/**
9
 * Test the functionality ElasticaService class
10
 * @package elastica
11
 */
12
class ElasticaServiceTest extends ElasticsearchBaseTest {
13
14
	public static $fixture_file = 'elastica/tests/lotsOfPhotos.yml';
15
16
17
	public function setUp() {
18
		parent::setUp();
19
		$this->service->setTestMode(true);
20
	}
21
22
23
	public function testCreateIndexInvalidLocale() {
24
		// fake locale
25
		$this->service->setLocale('sw_NZ');
26
		try {
27
			$this->invokeMethod($this->service, 'createIndex', array());
28
			$this->assertFalse(true, "Creation of index with unknown locale should have failed");
29
		} catch (Exception $e) {
30
			$this->assertTrue(true,"Creation of index with unknown locale failed as expected");
31
		}
32
33
34
	}
35
36
37
	public function testEnsureMapping() {
38
		$index = $this->service->getIndex();
39
		$mapping = $index->getMapping();
40
		$type = $index->getType('FlickrPhotoTO');
41
		$record = FlickrPhotoTO::get()->first();
42
		$mappingBefore = $this->invokeMethod($this->service, 'ensureMapping', array($type, $record));
43
44
		$this->assertEquals($mapping['FlickrPhotoTO'], $mappingBefore['FlickrPhotoTO']);
0 ignored issues
show
The method assertEquals() does not seem to exist on object<ElasticaServiceTest>.

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.

Loading history...
45
46
		// Delete the index
47
		$task = new DeleteIndexTask($this->service);
48
		$task->run(null);
49
		$mappingAfter = $this->invokeMethod($this->service, 'ensureMapping', array($type, $record));
50
		$this->assertEquals($mappingBefore, $mappingAfter);
0 ignored issues
show
The method assertEquals() does not seem to exist on object<ElasticaServiceTest>.

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.

Loading history...
51
	}
52
53
54
	public function testEnsureIndex() {
55
		// Check that an index currently exists, it will from setUp method
56
		$this->assertTrue($this->service->getIndex()->exists());
57
58
		// Ensure the index exists when it already exists case
59
		$this->invokeMethod($this->service, 'ensureIndex', array());
60
		$this->assertTrue($this->service->getIndex()->exists());
61
62
		// Delete and assert that it does not exist
63
		$this->service->getIndex()->delete();
64
		$this->assertFalse($this->service->getIndex()->exists());
65
66
		// Ensure the index exists when it does not exist case
67
		$this->invokeMethod($this->service, 'ensureIndex', array());
68
		$this->assertTrue($this->service->getIndex()->exists());
69
	}
70
71
72
	public function testShowInSearch() {
73
		$nDocsAtStart = $this->getNumberOfIndexedDocuments();
74
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
75
76
		$fp = Page::get()->first();
77
		$fp->ShowInSearch = false;
78
		$fp->write();
79
		$this->service->getIndex()->refresh();
80
81
		$this->checkNumberOfIndexedDocuments($nDocsAtStart-1);
82
83
		$fp->ShowInSearch = true;
84
		$fp->write();
85
		$this->service->getIndex()->refresh();
86
87
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
88
	}
89
90
91 View Code Duplication
	public function testUnpublish() {
92
		$nDocsAtStart = $this->getNumberOfIndexedDocuments();
93
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
94
95
		$fp = Page::get()->first();
96
		$fp->doUnpublish();
97
98
		$this->service->getIndex()->refresh();
99
		$this->checkNumberOfIndexedDocuments($nDocsAtStart-1);
100
	}
101
102
103 View Code Duplication
	public function testDeleteFromSiteTree() {
104
		$nDocsAtStart = $this->getNumberOfIndexedDocuments();
105
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
106
107
		$fp = Page::get()->first();
108
		$fp->delete();
109
110
		$this->service->getIndex()->refresh();
111
		$this->checkNumberOfIndexedDocuments($nDocsAtStart-1);
112
	}
113
114
115 View Code Duplication
	public function testDeleteDataObject() {
116
		$nDocsAtStart = $this->getNumberOfIndexedDocuments();
117
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
118
119
		$fp = FlickrPhotoTO::get()->first();
120
		$fp->delete();
121
122
		$this->service->getIndex()->refresh();
123
		$this->checkNumberOfIndexedDocuments($nDocsAtStart-1);
124
	}
125
126
127
	public function testBulkIndexing() {
128
		//Reset the index, so that nothing has been indexed
129
		$this->service->reset();
130
131
		//Number of requests indexing wise made to Elasticsearch server
132
		$reqs = $this->service->getIndexingRequestCtr();
133
134
		$task = new ReindexTask($this->service);
135
136
		// null request is fine as no parameters used
137
		$task->run(null);
138
139
		//Check that the number of indexing requests has increased by 2
140
		$deltaReqs = $this->service->getIndexingRequestCtr() - $reqs;
141
		//One call is made for each of Page and FlickrPhotoTO
142
		$this->assertEquals(2,$deltaReqs);
0 ignored issues
show
The method assertEquals() does not seem to exist on object<ElasticaServiceTest>.

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.

Loading history...
143
144
		// default installed pages plus 100 FlickrPhotoTOs
145
		$this->checkNumberOfIndexedDocuments(103);
146
	}
147
148
	public function testNonBulkIndexing() {
149
		//Number of requests indexing wise made to Elasticsearch server
150
		$reqs = $this->service->getIndexingRequestCtr();
151
		$nDocsAtStart = $this->getNumberOfIndexedDocuments();
152
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
153
154
		//Check index size after each document indexed
155
		$fp = new FlickrPhotoTO();
156
		$fp->Title = 'The cat sits on the mat';
0 ignored issues
show
The property Title does not exist on object<FlickrPhotoTO>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
157
		$fp->write();
158
		$this->checkNumberOfIndexedDocuments($nDocsAtStart+1);
159
160
		$fp2 = new FlickrPhotoTO();
161
		$fp2->Title = 'The cat sat on the hat';
0 ignored issues
show
The property Title does not exist on object<FlickrPhotoTO>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
162
		$fp2->write();
163
		$this->checkNumberOfIndexedDocuments($nDocsAtStart+2);
164
165
		$fp3 = new FlickrPhotoTO();
166
		$fp3->Title = 'The bat flew around the cat';
0 ignored issues
show
The property Title does not exist on object<FlickrPhotoTO>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
167
		$fp3->write();
168
		$this->checkNumberOfIndexedDocuments($nDocsAtStart+3);
169
170
		//Check that the number of indexing requests has increased by 3
171
		$deltaReqs = $this->service->getIndexingRequestCtr() - $reqs;
172
		$this->assertEquals(3,$deltaReqs);
0 ignored issues
show
The method assertEquals() does not seem to exist on object<ElasticaServiceTest>.

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.

Loading history...
173
	}
174
175
176
177
	/*
178
	Check for classes that should be there.   Others will possibly exist depending on
179
	which other modules are installed, hence no array comparison
180
	 */
181
	public function testGetIndexedClasses() {
182
		$this->service->setTestMode(false);
183
184
		$indexedClasses = $this->service->getIndexedClasses();
185
186
		// Just the non testable classes
187
		$this->assertContains('Page', $indexedClasses);
188
		$this->assertContains('SiteTree', $indexedClasses);
189
190
191
		// Get all classes including the test ones
192
		$this->service->setTestMode(true);
193
		$indexedClasses = $this->service->getIndexedClasses();
194
		$this->assertContains('Page', $indexedClasses);
195
		$this->assertContains('SiteTree', $indexedClasses);
196
		$this->assertContains('FlickrPhotoTO', $indexedClasses);
197
		$this->assertContains('FlickrSetTO', $indexedClasses);
198
		$this->assertContains('FlickrTagTO', $indexedClasses);
199
		$this->assertContains('SearchableTestPage', $indexedClasses);
200
201
	}
202
203
204
	/*
205
	-------------------------------
206
	FIXME: The following tests are problematic in that I'm not sure exactly what to check for
207
	to differentiate between deleted, reset, recreated etc.  It seems that checking an index
208
	status recreates a virgin copy of the index
209
	-------------------------------
210
	 */
211
212
	public function testResetIndex() {
213
		$index = $this->service->getIndex();
0 ignored issues
show
$index 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 $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
214
		$nDocsAtStart = $this->getNumberOfIndexedDocuments();
215
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
216
		$this->service->reset();
217
		$this->checkNumberOfIndexedDocuments(0);
218
	}
219
220
221
	public function testDeleteIndex() {
222
		$index = $this->service->getIndex();
223
		$this->assertTrue($index->exists());
224
225
		// Check the number of documents
226
		$nDocsAtStart = $this->getNumberOfIndexedDocuments();
227
		$this->checkNumberOfIndexedDocuments($nDocsAtStart);
228
229
230
		// Delete the index
231
		$task = new DeleteIndexTask($this->service);
232
233
		//null request is fine as no parameters used
234
		$task->run(null);
235
236
		$this->checkNumberOfIndexedDocuments(0);
237
238
		//FIXME better options for testing here?
239
	}
240
241
242
	public function testListIndexes() {
243
		$message = 'This is a test trace';
244
		$trace = $this->service->listIndexes($message);
245
		$this->assertContains('elastica_ss_module_test_en_us', print_r($trace,1));
246
	}
247
}
248