Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\simpletest\Tests;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Utility\Crypt;
|
Chris@0
|
6 use Drupal\Core\Test\TestDatabase;
|
Chris@0
|
7 use Drupal\simpletest\WebTestBase;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * Tests SimpleTest's web interface: check that the intended tests were run and
|
Chris@0
|
11 * ensure that test reports display the intended results. Also test SimpleTest's
|
Chris@0
|
12 * internal browser and APIs implicitly.
|
Chris@0
|
13 *
|
Chris@0
|
14 * @group simpletest
|
Chris@0
|
15 */
|
Chris@0
|
16 class SimpleTestTest extends WebTestBase {
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * Modules to enable.
|
Chris@0
|
20 *
|
Chris@0
|
21 * @var array
|
Chris@0
|
22 */
|
Chris@0
|
23 public static $modules = ['simpletest'];
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * The results array that has been parsed by getTestResults().
|
Chris@0
|
27 *
|
Chris@0
|
28 * @var array
|
Chris@0
|
29 */
|
Chris@0
|
30 protected $childTestResults;
|
Chris@0
|
31
|
Chris@0
|
32 /**
|
Chris@0
|
33 * Stores the test ID from each test run for comparison.
|
Chris@0
|
34 *
|
Chris@0
|
35 * Used to ensure they are incrementing.
|
Chris@0
|
36 */
|
Chris@0
|
37 protected $testIds = [];
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * Translated fail message.
|
Chris@0
|
41 *
|
Chris@0
|
42 * @var string
|
Chris@0
|
43 */
|
Chris@0
|
44 private $failMessage = '';
|
Chris@0
|
45
|
Chris@0
|
46 /**
|
Chris@0
|
47 * Translated pass message.
|
Chris@0
|
48 * @var string
|
Chris@0
|
49 */
|
Chris@0
|
50 private $passMessage = '';
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * A valid and recognized permission.
|
Chris@0
|
54 *
|
Chris@0
|
55 * @var string
|
Chris@0
|
56 */
|
Chris@0
|
57 protected $validPermission;
|
Chris@0
|
58
|
Chris@0
|
59 /**
|
Chris@0
|
60 * An invalid or unrecognized permission.
|
Chris@0
|
61 *
|
Chris@0
|
62 * @var string
|
Chris@0
|
63 */
|
Chris@0
|
64 protected $invalidPermission;
|
Chris@0
|
65
|
Chris@0
|
66 protected function setUp() {
|
Chris@0
|
67 if (!$this->isInChildSite()) {
|
Chris@0
|
68 $php = <<<'EOD'
|
Chris@0
|
69 <?php
|
Chris@0
|
70
|
Chris@0
|
71 # Make sure that the $test_class variable is defined when this file is included.
|
Chris@0
|
72 if ($test_class) {
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 # Define a function to be able to check that this file was loaded with
|
Chris@0
|
76 # function_exists().
|
Chris@0
|
77 if (!function_exists('simpletest_test_stub_settings_function')) {
|
Chris@0
|
78 function simpletest_test_stub_settings_function() {}
|
Chris@0
|
79 }
|
Chris@0
|
80 EOD;
|
Chris@0
|
81
|
Chris@0
|
82 file_put_contents($this->siteDirectory . '/' . 'settings.testing.php', $php);
|
Chris@0
|
83 // @see \Drupal\system\Tests\DrupalKernel\DrupalKernelSiteTest
|
Chris@0
|
84 $class = __CLASS__;
|
Chris@0
|
85 $yaml = <<<EOD
|
Chris@0
|
86 services:
|
Chris@0
|
87 # Add a new service.
|
Chris@0
|
88 site.service.yml:
|
Chris@0
|
89 class: $class
|
Chris@0
|
90 # Swap out a core service.
|
Chris@0
|
91 cache.backend.database:
|
Chris@0
|
92 class: Drupal\Core\Cache\MemoryBackendFactory
|
Chris@0
|
93 EOD;
|
Chris@0
|
94 file_put_contents($this->siteDirectory . '/testing.services.yml', $yaml);
|
Chris@0
|
95
|
Chris@0
|
96 $original_container = $this->originalContainer;
|
Chris@0
|
97 parent::setUp();
|
Chris@0
|
98 $this->assertNotIdentical(\Drupal::getContainer(), $original_container, 'WebTestBase test creates a new container.');
|
Chris@0
|
99 // Create and log in an admin user.
|
Chris@0
|
100 $this->drupalLogin($this->drupalCreateUser(['administer unit tests']));
|
Chris@0
|
101 }
|
Chris@0
|
102 else {
|
Chris@0
|
103 // This causes three of the five fails that are asserted in
|
Chris@0
|
104 // confirmStubResults().
|
Chris@0
|
105 self::$modules = ['non_existent_module'];
|
Chris@0
|
106 parent::setUp();
|
Chris@0
|
107 }
|
Chris@0
|
108 }
|
Chris@0
|
109
|
Chris@0
|
110 /**
|
Chris@0
|
111 * Ensures the tests selected through the web interface are run and displayed.
|
Chris@0
|
112 */
|
Chris@0
|
113 public function testWebTestRunner() {
|
Chris@0
|
114 $this->passMessage = t('SimpleTest pass.');
|
Chris@0
|
115 $this->failMessage = t('SimpleTest fail.');
|
Chris@0
|
116 $this->validPermission = 'access administration pages';
|
Chris@0
|
117 $this->invalidPermission = 'invalid permission';
|
Chris@0
|
118
|
Chris@0
|
119 if ($this->isInChildSite()) {
|
Chris@0
|
120 // Only run following code if this test is running itself through a CURL
|
Chris@0
|
121 // request.
|
Chris@0
|
122 $this->stubTest();
|
Chris@0
|
123 }
|
Chris@0
|
124 else {
|
Chris@0
|
125 // Run twice so test_ids can be accumulated.
|
Chris@0
|
126 for ($i = 0; $i < 2; $i++) {
|
Chris@0
|
127 // Run this test from web interface.
|
Chris@0
|
128 $this->drupalGet('admin/config/development/testing');
|
Chris@0
|
129
|
Chris@0
|
130 $edit = [];
|
Chris@0
|
131 $edit['tests[Drupal\simpletest\Tests\SimpleTestTest]'] = TRUE;
|
Chris@0
|
132 $this->drupalPostForm(NULL, $edit, t('Run tests'));
|
Chris@0
|
133
|
Chris@0
|
134 // Parse results and confirm that they are correct.
|
Chris@0
|
135 $this->getTestResults();
|
Chris@0
|
136 $this->confirmStubTestResults();
|
Chris@0
|
137 }
|
Chris@0
|
138
|
Chris@0
|
139 // Regression test for #290316.
|
Chris@0
|
140 // Check that test_id is incrementing.
|
Chris@0
|
141 $this->assertTrue($this->testIds[0] != $this->testIds[1], 'Test ID is incrementing.');
|
Chris@0
|
142 }
|
Chris@0
|
143 }
|
Chris@0
|
144
|
Chris@0
|
145 /**
|
Chris@0
|
146 * Test to be run and the results confirmed.
|
Chris@0
|
147 *
|
Chris@0
|
148 * Here we force test results which must match the expected results from
|
Chris@0
|
149 * confirmStubResults().
|
Chris@0
|
150 */
|
Chris@0
|
151 public function stubTest() {
|
Chris@0
|
152 // Ensure the .htkey file exists since this is only created just before a
|
Chris@0
|
153 // request. This allows the stub test to make requests. The event does not
|
Chris@0
|
154 // fire here and drupal_generate_test_ua() can not generate a key for a
|
Chris@0
|
155 // test in a test since the prefix has changed.
|
Chris@0
|
156 // @see \Drupal\Core\Test\HttpClientMiddleware\TestHttpClientMiddleware::onBeforeSendRequest()
|
Chris@0
|
157 // @see drupal_generate_test_ua();
|
Chris@0
|
158 $test_db = new TestDatabase($this->databasePrefix);
|
Chris@0
|
159 $key_file = DRUPAL_ROOT . '/' . $test_db->getTestSitePath() . '/.htkey';
|
Chris@0
|
160 $private_key = Crypt::randomBytesBase64(55);
|
Chris@0
|
161 $site_path = $this->container->get('site.path');
|
Chris@0
|
162 file_put_contents($key_file, $private_key);
|
Chris@0
|
163
|
Chris@0
|
164 // Check to see if runtime assertions are indeed on, if successful this
|
Chris@0
|
165 // will be the first of sixteen passes asserted in confirmStubResults()
|
Chris@0
|
166 try {
|
Chris@0
|
167 // Test with minimum possible arguments to make sure no notice for
|
Chris@0
|
168 // missing argument is thrown.
|
Chris@0
|
169 assert(FALSE);
|
Chris@0
|
170 $this->fail('Runtime assertions are not working.');
|
Chris@0
|
171 }
|
Chris@0
|
172 catch (\AssertionError $e) {
|
Chris@0
|
173 try {
|
Chris@0
|
174 // Now test with an error message to ensure it is correctly passed
|
Chris@0
|
175 // along by the rethrow.
|
Chris@0
|
176 assert(FALSE, 'Lorem Ipsum');
|
Chris@0
|
177 }
|
Chris@0
|
178 catch (\AssertionError $e) {
|
Chris@0
|
179 $this->assertEqual($e->getMessage(), 'Lorem Ipsum', 'Runtime assertions Enabled and running.');
|
Chris@0
|
180 }
|
Chris@0
|
181 }
|
Chris@0
|
182 // This causes the second of the sixteen passes asserted in
|
Chris@0
|
183 // confirmStubResults().
|
Chris@0
|
184 $this->pass($this->passMessage);
|
Chris@0
|
185
|
Chris@0
|
186 // The first three fails are caused by enabling a non-existent module in
|
Chris@0
|
187 // setUp().
|
Chris@0
|
188
|
Chris@0
|
189 // This causes the fourth of the five fails asserted in
|
Chris@0
|
190 // confirmStubResults().
|
Chris@0
|
191 $this->fail($this->failMessage);
|
Chris@0
|
192
|
Chris@0
|
193 // This causes the third to fifth of the sixteen passes asserted in
|
Chris@0
|
194 // confirmStubResults().
|
Chris@0
|
195 $user = $this->drupalCreateUser([$this->validPermission], 'SimpleTestTest');
|
Chris@0
|
196
|
Chris@0
|
197 // This causes the fifth of the five fails asserted in confirmStubResults().
|
Chris@0
|
198 $this->drupalCreateUser([$this->invalidPermission]);
|
Chris@0
|
199
|
Chris@0
|
200 // Test logging in as a user.
|
Chris@0
|
201 // This causes the sixth to tenth of the sixteen passes asserted in
|
Chris@0
|
202 // confirmStubResults().
|
Chris@0
|
203 $this->drupalLogin($user);
|
Chris@0
|
204
|
Chris@0
|
205 // This causes the eleventh of the sixteen passes asserted in
|
Chris@0
|
206 // confirmStubResults().
|
Chris@0
|
207 $this->pass(t('Test ID is @id.', ['@id' => $this->testId]));
|
Chris@0
|
208
|
Chris@0
|
209 // These cause the twelfth to fifteenth of the sixteen passes asserted in
|
Chris@0
|
210 // confirmStubResults().
|
Chris@0
|
211 $this->assertTrue(file_exists($site_path . '/settings.testing.php'));
|
Chris@0
|
212 // Check the settings.testing.php file got included.
|
Chris@0
|
213 $this->assertTrue(function_exists('simpletest_test_stub_settings_function'));
|
Chris@0
|
214 // Check that the test-specific service file got loaded.
|
Chris@0
|
215 $this->assertTrue($this->container->has('site.service.yml'));
|
Chris@0
|
216 $this->assertIdentical(get_class($this->container->get('cache.backend.database')), 'Drupal\Core\Cache\MemoryBackendFactory');
|
Chris@0
|
217
|
Chris@0
|
218 // These cause the two exceptions asserted in confirmStubResults().
|
Chris@0
|
219 // Call trigger_error() without the required argument to trigger an E_WARNING.
|
Chris@0
|
220 trigger_error();
|
Chris@0
|
221 // Generates a warning inside a PHP function.
|
Chris@0
|
222 array_key_exists(NULL, NULL);
|
Chris@0
|
223
|
Chris@0
|
224 // This causes the sixteenth of the sixteen passes asserted in
|
Chris@0
|
225 // confirmStubResults().
|
Chris@0
|
226 $this->assertNothing();
|
Chris@0
|
227
|
Chris@0
|
228 // This causes the debug message asserted in confirmStubResults().
|
Chris@0
|
229 debug('Foo', 'Debug', FALSE);
|
Chris@0
|
230 }
|
Chris@0
|
231
|
Chris@0
|
232 /**
|
Chris@0
|
233 * Assert nothing.
|
Chris@0
|
234 */
|
Chris@0
|
235 public function assertNothing() {
|
Chris@0
|
236 $this->pass("This is nothing.");
|
Chris@0
|
237 }
|
Chris@0
|
238
|
Chris@0
|
239 /**
|
Chris@0
|
240 * Confirm that the stub test produced the desired results.
|
Chris@0
|
241 */
|
Chris@0
|
242 public function confirmStubTestResults() {
|
Chris@0
|
243 $this->assertAssertion(t('Unable to install modules %modules due to missing modules %missing.', ['%modules' => 'non_existent_module', '%missing' => 'non_existent_module']), 'Other', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->setUp()');
|
Chris@0
|
244
|
Chris@0
|
245 $this->assertAssertion($this->passMessage, 'Other', 'Pass', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
|
Chris@0
|
246 $this->assertAssertion($this->failMessage, 'Other', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
|
Chris@0
|
247
|
Chris@0
|
248 $this->assertAssertion(t('Created permissions: @perms', ['@perms' => $this->validPermission]), 'Role', 'Pass', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
|
Chris@0
|
249 $this->assertAssertion(t('Invalid permission %permission.', ['%permission' => $this->invalidPermission]), 'Role', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
|
Chris@0
|
250
|
Chris@0
|
251 // Check that the user was logged in successfully.
|
Chris@0
|
252 $this->assertAssertion('User SimpleTestTest successfully logged in.', 'User login', 'Pass', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
|
Chris@0
|
253
|
Chris@0
|
254 // Check that a warning is caught by simpletest. The exact error message
|
Chris@0
|
255 // differs between PHP versions so only the function name is checked.
|
Chris@0
|
256 $this->assertAssertion('trigger_error()', 'Warning', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
|
Chris@0
|
257
|
Chris@0
|
258 // Check that the backtracing code works for specific assert function.
|
Chris@0
|
259 $this->assertAssertion('This is nothing.', 'Other', 'Pass', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
|
Chris@0
|
260
|
Chris@0
|
261 // Check that errors that occur inside PHP internal functions are correctly
|
Chris@0
|
262 // reported. The exact error message differs between PHP versions so we
|
Chris@0
|
263 // check only the function name 'array_key_exists'.
|
Chris@0
|
264 $this->assertAssertion('array_key_exists', 'Warning', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
|
Chris@0
|
265
|
Chris@0
|
266 $this->assertAssertion("Debug: 'Foo'", 'Debug', 'Fail', 'SimpleTestTest.php', 'Drupal\simpletest\Tests\SimpleTestTest->stubTest()');
|
Chris@0
|
267
|
Chris@0
|
268 $this->assertEqual('16 passes, 3 fails, 2 exceptions, 3 debug messages', $this->childTestResults['summary']);
|
Chris@0
|
269
|
Chris@0
|
270 $this->testIds[] = $test_id = $this->getTestIdFromResults();
|
Chris@0
|
271 $this->assertTrue($test_id, 'Found test ID in results.');
|
Chris@0
|
272 }
|
Chris@0
|
273
|
Chris@0
|
274 /**
|
Chris@0
|
275 * Fetch the test id from the test results.
|
Chris@0
|
276 */
|
Chris@0
|
277 public function getTestIdFromResults() {
|
Chris@0
|
278 foreach ($this->childTestResults['assertions'] as $assertion) {
|
Chris@0
|
279 if (preg_match('@^Test ID is ([0-9]*)\.$@', $assertion['message'], $matches)) {
|
Chris@0
|
280 return $matches[1];
|
Chris@0
|
281 }
|
Chris@0
|
282 }
|
Chris@0
|
283 return NULL;
|
Chris@0
|
284 }
|
Chris@0
|
285
|
Chris@0
|
286 /**
|
Chris@0
|
287 * Asserts that an assertion with specified values is displayed in results.
|
Chris@0
|
288 *
|
Chris@0
|
289 * @param string $message
|
Chris@0
|
290 * Assertion message.
|
Chris@0
|
291 * @param string $type
|
Chris@0
|
292 * Assertion type.
|
Chris@0
|
293 * @param string $status
|
Chris@0
|
294 * Assertion status.
|
Chris@0
|
295 * @param string $file
|
Chris@0
|
296 * File where the assertion originated.
|
Chris@0
|
297 * @param string $function
|
Chris@0
|
298 * Function where the assertion originated.
|
Chris@0
|
299 *
|
Chris@0
|
300 * @return Assertion result.
|
Chris@0
|
301 */
|
Chris@0
|
302 public function assertAssertion($message, $type, $status, $file, $function) {
|
Chris@0
|
303 $message = trim(strip_tags($message));
|
Chris@0
|
304 $found = FALSE;
|
Chris@0
|
305 foreach ($this->childTestResults['assertions'] as $assertion) {
|
Chris@0
|
306 if ((strpos($assertion['message'], $message) !== FALSE) &&
|
Chris@0
|
307 $assertion['type'] == $type &&
|
Chris@0
|
308 $assertion['status'] == $status &&
|
Chris@0
|
309 $assertion['file'] == $file &&
|
Chris@0
|
310 $assertion['function'] == $function) {
|
Chris@0
|
311 $found = TRUE;
|
Chris@0
|
312 break;
|
Chris@0
|
313 }
|
Chris@0
|
314 }
|
Chris@0
|
315 return $this->assertTrue($found, format_string('Found assertion {"@message", "@type", "@status", "@file", "@function"}.', ['@message' => $message, '@type' => $type, '@status' => $status, "@file" => $file, "@function" => $function]));
|
Chris@0
|
316 }
|
Chris@0
|
317
|
Chris@0
|
318 /**
|
Chris@0
|
319 * Get the results from a test and store them in the class array $results.
|
Chris@0
|
320 */
|
Chris@0
|
321 public function getTestResults() {
|
Chris@0
|
322 $results = [];
|
Chris@0
|
323 if ($this->parse()) {
|
Chris@0
|
324 if ($details = $this->getResultFieldSet()) {
|
Chris@0
|
325 // Code assumes this is the only test in group.
|
Chris@0
|
326 $results['summary'] = $this->asText($details->div->div[1]);
|
Chris@0
|
327 $results['name'] = $this->asText($details->summary);
|
Chris@0
|
328
|
Chris@0
|
329 $results['assertions'] = [];
|
Chris@0
|
330 $tbody = $details->div->table->tbody;
|
Chris@0
|
331 foreach ($tbody->tr as $row) {
|
Chris@0
|
332 $assertion = [];
|
Chris@0
|
333 $assertion['message'] = $this->asText($row->td[0]);
|
Chris@0
|
334 $assertion['type'] = $this->asText($row->td[1]);
|
Chris@0
|
335 $assertion['file'] = $this->asText($row->td[2]);
|
Chris@0
|
336 $assertion['line'] = $this->asText($row->td[3]);
|
Chris@0
|
337 $assertion['function'] = $this->asText($row->td[4]);
|
Chris@0
|
338 $ok_url = file_url_transform_relative(file_create_url('core/misc/icons/73b355/check.svg'));
|
Chris@0
|
339 $assertion['status'] = ($row->td[5]->img['src'] == $ok_url) ? 'Pass' : 'Fail';
|
Chris@0
|
340 $results['assertions'][] = $assertion;
|
Chris@0
|
341 }
|
Chris@0
|
342 }
|
Chris@0
|
343 }
|
Chris@0
|
344 $this->childTestResults = $results;
|
Chris@0
|
345 }
|
Chris@0
|
346
|
Chris@0
|
347 /**
|
Chris@0
|
348 * Get the details containing the results for group this test is in.
|
Chris@0
|
349 */
|
Chris@0
|
350 public function getResultFieldSet() {
|
Chris@0
|
351 $all_details = $this->xpath('//details');
|
Chris@0
|
352 foreach ($all_details as $details) {
|
Chris@0
|
353 if ($this->asText($details->summary) == __CLASS__) {
|
Chris@0
|
354 return $details;
|
Chris@0
|
355 }
|
Chris@0
|
356 }
|
Chris@0
|
357 return FALSE;
|
Chris@0
|
358 }
|
Chris@0
|
359
|
Chris@0
|
360 /**
|
Chris@0
|
361 * Extract the text contained by the element.
|
Chris@0
|
362 *
|
Chris@0
|
363 * @param $element
|
Chris@0
|
364 * Element to extract text from.
|
Chris@0
|
365 *
|
Chris@0
|
366 * @return
|
Chris@0
|
367 * Extracted text.
|
Chris@0
|
368 */
|
Chris@0
|
369 public function asText(\SimpleXMLElement $element) {
|
Chris@0
|
370 if (!is_object($element)) {
|
Chris@0
|
371 return $this->fail('The element is not an element.');
|
Chris@0
|
372 }
|
Chris@0
|
373 return trim(html_entity_decode(strip_tags($element->asXML())));
|
Chris@0
|
374 }
|
Chris@0
|
375
|
Chris@0
|
376 }
|