Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests;
|
Chris@0
|
4
|
Chris@0
|
5 use Behat\Mink\Driver\GoutteDriver;
|
Chris@0
|
6 use Behat\Mink\Element\Element;
|
Chris@0
|
7 use Behat\Mink\Mink;
|
Chris@0
|
8 use Behat\Mink\Selector\SelectorsHandler;
|
Chris@0
|
9 use Behat\Mink\Session;
|
Chris@0
|
10 use Drupal\Component\Serialization\Json;
|
Chris@0
|
11 use Drupal\Core\Database\Database;
|
Chris@0
|
12 use Drupal\Core\Test\FunctionalTestSetupTrait;
|
Chris@0
|
13 use Drupal\Core\Test\TestSetupTrait;
|
Chris@0
|
14 use Drupal\Core\Utility\Error;
|
Chris@0
|
15 use Drupal\FunctionalTests\AssertLegacyTrait;
|
Chris@0
|
16 use Drupal\Tests\block\Traits\BlockCreationTrait;
|
Chris@0
|
17 use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
|
Chris@0
|
18 use Drupal\Tests\node\Traits\NodeCreationTrait;
|
Chris@0
|
19 use Drupal\Tests\user\Traits\UserCreationTrait;
|
Chris@17
|
20 use GuzzleHttp\Cookie\CookieJar;
|
Chris@0
|
21 use PHPUnit\Framework\TestCase;
|
Chris@0
|
22 use Psr\Http\Message\RequestInterface;
|
Chris@0
|
23 use Psr\Http\Message\ResponseInterface;
|
Chris@0
|
24 use Symfony\Component\CssSelector\CssSelectorConverter;
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * Provides a test case for functional Drupal tests.
|
Chris@0
|
28 *
|
Chris@0
|
29 * Tests extending BrowserTestBase must exist in the
|
Chris@0
|
30 * Drupal\Tests\yourmodule\Functional namespace and live in the
|
Chris@0
|
31 * modules/yourmodule/tests/src/Functional directory.
|
Chris@0
|
32 *
|
Chris@12
|
33 * Tests extending this base class should only translate text when testing
|
Chris@12
|
34 * translation functionality. For example, avoid wrapping test text with t()
|
Chris@12
|
35 * or TranslatableMarkup().
|
Chris@12
|
36 *
|
Chris@0
|
37 * @ingroup testing
|
Chris@0
|
38 */
|
Chris@0
|
39 abstract class BrowserTestBase extends TestCase {
|
Chris@0
|
40
|
Chris@0
|
41 use FunctionalTestSetupTrait;
|
Chris@17
|
42 use UiHelperTrait {
|
Chris@17
|
43 FunctionalTestSetupTrait::refreshVariables insteadof UiHelperTrait;
|
Chris@17
|
44 }
|
Chris@0
|
45 use TestSetupTrait;
|
Chris@0
|
46 use BlockCreationTrait {
|
Chris@0
|
47 placeBlock as drupalPlaceBlock;
|
Chris@0
|
48 }
|
Chris@0
|
49 use AssertLegacyTrait;
|
Chris@0
|
50 use RandomGeneratorTrait;
|
Chris@0
|
51 use NodeCreationTrait {
|
Chris@0
|
52 getNodeByTitle as drupalGetNodeByTitle;
|
Chris@0
|
53 createNode as drupalCreateNode;
|
Chris@0
|
54 }
|
Chris@0
|
55 use ContentTypeCreationTrait {
|
Chris@0
|
56 createContentType as drupalCreateContentType;
|
Chris@0
|
57 }
|
Chris@0
|
58 use ConfigTestTrait;
|
Chris@0
|
59 use TestRequirementsTrait;
|
Chris@0
|
60 use UserCreationTrait {
|
Chris@0
|
61 createRole as drupalCreateRole;
|
Chris@0
|
62 createUser as drupalCreateUser;
|
Chris@0
|
63 }
|
Chris@0
|
64 use XdebugRequestTrait;
|
Chris@12
|
65 use PhpunitCompatibilityTrait;
|
Chris@0
|
66
|
Chris@0
|
67 /**
|
Chris@0
|
68 * The database prefix of this test run.
|
Chris@0
|
69 *
|
Chris@0
|
70 * @var string
|
Chris@0
|
71 */
|
Chris@0
|
72 protected $databasePrefix;
|
Chris@0
|
73
|
Chris@0
|
74 /**
|
Chris@0
|
75 * Time limit in seconds for the test.
|
Chris@0
|
76 *
|
Chris@0
|
77 * @var int
|
Chris@0
|
78 */
|
Chris@0
|
79 protected $timeLimit = 500;
|
Chris@0
|
80
|
Chris@0
|
81 /**
|
Chris@0
|
82 * The translation file directory for the test environment.
|
Chris@0
|
83 *
|
Chris@0
|
84 * This is set in BrowserTestBase::prepareEnvironment().
|
Chris@0
|
85 *
|
Chris@0
|
86 * @var string
|
Chris@0
|
87 */
|
Chris@0
|
88 protected $translationFilesDirectory;
|
Chris@0
|
89
|
Chris@0
|
90 /**
|
Chris@0
|
91 * The config importer that can be used in a test.
|
Chris@0
|
92 *
|
Chris@0
|
93 * @var \Drupal\Core\Config\ConfigImporter
|
Chris@0
|
94 */
|
Chris@0
|
95 protected $configImporter;
|
Chris@0
|
96
|
Chris@0
|
97 /**
|
Chris@0
|
98 * Modules to enable.
|
Chris@0
|
99 *
|
Chris@0
|
100 * The test runner will merge the $modules lists from this class, the class
|
Chris@0
|
101 * it extends, and so on up the class hierarchy. It is not necessary to
|
Chris@0
|
102 * include modules in your list that a parent class has already declared.
|
Chris@0
|
103 *
|
Chris@0
|
104 * @var string[]
|
Chris@0
|
105 *
|
Chris@0
|
106 * @see \Drupal\Tests\BrowserTestBase::installDrupal()
|
Chris@0
|
107 */
|
Chris@0
|
108 protected static $modules = [];
|
Chris@0
|
109
|
Chris@0
|
110 /**
|
Chris@0
|
111 * The profile to install as a basis for testing.
|
Chris@0
|
112 *
|
Chris@0
|
113 * @var string
|
Chris@0
|
114 */
|
Chris@0
|
115 protected $profile = 'testing';
|
Chris@0
|
116
|
Chris@0
|
117 /**
|
Chris@0
|
118 * An array of custom translations suitable for drupal_rewrite_settings().
|
Chris@0
|
119 *
|
Chris@0
|
120 * @var array
|
Chris@0
|
121 */
|
Chris@0
|
122 protected $customTranslations;
|
Chris@0
|
123
|
Chris@0
|
124 /*
|
Chris@0
|
125 * Mink class for the default driver to use.
|
Chris@0
|
126 *
|
Chris@17
|
127 * Should be a fully-qualified class name that implements
|
Chris@0
|
128 * Behat\Mink\Driver\DriverInterface.
|
Chris@0
|
129 *
|
Chris@0
|
130 * Value can be overridden using the environment variable MINK_DRIVER_CLASS.
|
Chris@0
|
131 *
|
Chris@17
|
132 * @var string
|
Chris@0
|
133 */
|
Chris@0
|
134 protected $minkDefaultDriverClass = GoutteDriver::class;
|
Chris@0
|
135
|
Chris@0
|
136 /*
|
Chris@0
|
137 * Mink default driver params.
|
Chris@0
|
138 *
|
Chris@0
|
139 * If it's an array its contents are used as constructor params when default
|
Chris@0
|
140 * Mink driver class is instantiated.
|
Chris@0
|
141 *
|
Chris@0
|
142 * Can be overridden using the environment variable MINK_DRIVER_ARGS. In this
|
Chris@0
|
143 * case that variable should be a JSON array, for example:
|
Chris@0
|
144 * '["firefox", null, "http://localhost:4444/wd/hub"]'.
|
Chris@0
|
145 *
|
Chris@0
|
146 *
|
Chris@0
|
147 * @var array
|
Chris@0
|
148 */
|
Chris@0
|
149 protected $minkDefaultDriverArgs;
|
Chris@0
|
150
|
Chris@0
|
151 /**
|
Chris@0
|
152 * Mink session manager.
|
Chris@0
|
153 *
|
Chris@0
|
154 * This will not be initialized if there was an error during the test setup.
|
Chris@0
|
155 *
|
Chris@0
|
156 * @var \Behat\Mink\Mink|null
|
Chris@0
|
157 */
|
Chris@0
|
158 protected $mink;
|
Chris@0
|
159
|
Chris@0
|
160 /**
|
Chris@0
|
161 * {@inheritdoc}
|
Chris@0
|
162 *
|
Chris@0
|
163 * Browser tests are run in separate processes to prevent collisions between
|
Chris@0
|
164 * code that may be loaded by tests.
|
Chris@0
|
165 */
|
Chris@0
|
166 protected $runTestInSeparateProcess = TRUE;
|
Chris@0
|
167
|
Chris@0
|
168 /**
|
Chris@0
|
169 * {@inheritdoc}
|
Chris@0
|
170 */
|
Chris@0
|
171 protected $preserveGlobalState = FALSE;
|
Chris@0
|
172
|
Chris@0
|
173 /**
|
Chris@0
|
174 * The base URL.
|
Chris@0
|
175 *
|
Chris@0
|
176 * @var string
|
Chris@0
|
177 */
|
Chris@0
|
178 protected $baseUrl;
|
Chris@0
|
179
|
Chris@0
|
180 /**
|
Chris@0
|
181 * The original array of shutdown function callbacks.
|
Chris@0
|
182 *
|
Chris@0
|
183 * @var array
|
Chris@0
|
184 */
|
Chris@0
|
185 protected $originalShutdownCallbacks = [];
|
Chris@0
|
186
|
Chris@0
|
187 /**
|
Chris@0
|
188 * The app root.
|
Chris@0
|
189 *
|
Chris@0
|
190 * @var string
|
Chris@0
|
191 */
|
Chris@0
|
192 protected $root;
|
Chris@0
|
193
|
Chris@0
|
194 /**
|
Chris@0
|
195 * The original container.
|
Chris@0
|
196 *
|
Chris@0
|
197 * Move this to \Drupal\Core\Test\FunctionalTestSetupTrait once TestBase no
|
Chris@0
|
198 * longer provides the same value.
|
Chris@0
|
199 *
|
Chris@0
|
200 * @var \Symfony\Component\DependencyInjection\ContainerInterface
|
Chris@0
|
201 */
|
Chris@0
|
202 protected $originalContainer;
|
Chris@0
|
203
|
Chris@0
|
204 /**
|
Chris@0
|
205 * {@inheritdoc}
|
Chris@0
|
206 */
|
Chris@0
|
207 public function __construct($name = NULL, array $data = [], $dataName = '') {
|
Chris@0
|
208 parent::__construct($name, $data, $dataName);
|
Chris@0
|
209
|
Chris@0
|
210 $this->root = dirname(dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))));
|
Chris@0
|
211 }
|
Chris@0
|
212
|
Chris@0
|
213 /**
|
Chris@0
|
214 * Initializes Mink sessions.
|
Chris@0
|
215 */
|
Chris@0
|
216 protected function initMink() {
|
Chris@0
|
217 $driver = $this->getDefaultDriverInstance();
|
Chris@0
|
218
|
Chris@0
|
219 if ($driver instanceof GoutteDriver) {
|
Chris@0
|
220 // Turn off curl timeout. Having a timeout is not a problem in a normal
|
Chris@0
|
221 // test running, but it is a problem when debugging. Also, disable SSL
|
Chris@0
|
222 // peer verification so that testing under HTTPS always works.
|
Chris@0
|
223 /** @var \GuzzleHttp\Client $client */
|
Chris@0
|
224 $client = $this->container->get('http_client_factory')->fromOptions([
|
Chris@0
|
225 'timeout' => NULL,
|
Chris@0
|
226 'verify' => FALSE,
|
Chris@0
|
227 ]);
|
Chris@0
|
228
|
Chris@0
|
229 // Inject a Guzzle middleware to generate debug output for every request
|
Chris@0
|
230 // performed in the test.
|
Chris@0
|
231 $handler_stack = $client->getConfig('handler');
|
Chris@0
|
232 $handler_stack->push($this->getResponseLogHandler());
|
Chris@0
|
233
|
Chris@0
|
234 $driver->getClient()->setClient($client);
|
Chris@0
|
235 }
|
Chris@0
|
236
|
Chris@0
|
237 $selectors_handler = new SelectorsHandler([
|
Chris@17
|
238 'hidden_field_selector' => new HiddenFieldSelector(),
|
Chris@0
|
239 ]);
|
Chris@0
|
240 $session = new Session($driver, $selectors_handler);
|
Chris@0
|
241 $this->mink = new Mink();
|
Chris@0
|
242 $this->mink->registerSession('default', $session);
|
Chris@0
|
243 $this->mink->setDefaultSessionName('default');
|
Chris@0
|
244 $this->registerSessions();
|
Chris@0
|
245
|
Chris@16
|
246 $this->initFrontPage();
|
Chris@16
|
247
|
Chris@17
|
248 // Copies cookies from the current environment, for example, XDEBUG_SESSION
|
Chris@17
|
249 // in order to support Xdebug.
|
Chris@17
|
250 // @see BrowserTestBase::initFrontPage()
|
Chris@17
|
251 $cookies = $this->extractCookiesFromRequest(\Drupal::request());
|
Chris@17
|
252 foreach ($cookies as $cookie_name => $values) {
|
Chris@17
|
253 foreach ($values as $value) {
|
Chris@17
|
254 $session->setCookie($cookie_name, $value);
|
Chris@17
|
255 }
|
Chris@17
|
256 }
|
Chris@17
|
257
|
Chris@16
|
258 return $session;
|
Chris@16
|
259 }
|
Chris@16
|
260
|
Chris@16
|
261 /**
|
Chris@16
|
262 * Visits the front page when initializing Mink.
|
Chris@16
|
263 *
|
Chris@16
|
264 * According to the W3C WebDriver specification a cookie can only be set if
|
Chris@16
|
265 * the cookie domain is equal to the domain of the active document. When the
|
Chris@16
|
266 * browser starts up the active document is not our domain but 'about:blank'
|
Chris@16
|
267 * or similar. To be able to set our User-Agent and Xdebug cookies at the
|
Chris@16
|
268 * start of the test we now do a request to the front page so the active
|
Chris@16
|
269 * document matches the domain.
|
Chris@16
|
270 *
|
Chris@16
|
271 * @see https://w3c.github.io/webdriver/webdriver-spec.html#add-cookie
|
Chris@16
|
272 * @see https://www.w3.org/Bugs/Public/show_bug.cgi?id=20975
|
Chris@16
|
273 */
|
Chris@16
|
274 protected function initFrontPage() {
|
Chris@0
|
275 $session = $this->getSession();
|
Chris@0
|
276 $session->visit($this->baseUrl);
|
Chris@0
|
277 }
|
Chris@0
|
278
|
Chris@0
|
279 /**
|
Chris@0
|
280 * Gets an instance of the default Mink driver.
|
Chris@0
|
281 *
|
Chris@0
|
282 * @return Behat\Mink\Driver\DriverInterface
|
Chris@0
|
283 * Instance of default Mink driver.
|
Chris@0
|
284 *
|
Chris@0
|
285 * @throws \InvalidArgumentException
|
Chris@0
|
286 * When provided default Mink driver class can't be instantiated.
|
Chris@0
|
287 */
|
Chris@0
|
288 protected function getDefaultDriverInstance() {
|
Chris@14
|
289 // Get default driver params from environment if available.
|
Chris@14
|
290 if ($arg_json = $this->getMinkDriverArgs()) {
|
Chris@12
|
291 $this->minkDefaultDriverArgs = json_decode($arg_json, TRUE);
|
Chris@0
|
292 }
|
Chris@0
|
293
|
Chris@14
|
294 // Get and check default driver class from environment if available.
|
Chris@0
|
295 if ($minkDriverClass = getenv('MINK_DRIVER_CLASS')) {
|
Chris@0
|
296 if (class_exists($minkDriverClass)) {
|
Chris@0
|
297 $this->minkDefaultDriverClass = $minkDriverClass;
|
Chris@0
|
298 }
|
Chris@0
|
299 else {
|
Chris@0
|
300 throw new \InvalidArgumentException("Can't instantiate provided $minkDriverClass class by environment as default driver class.");
|
Chris@0
|
301 }
|
Chris@0
|
302 }
|
Chris@0
|
303
|
Chris@0
|
304 if (is_array($this->minkDefaultDriverArgs)) {
|
Chris@0
|
305 // Use ReflectionClass to instantiate class with received params.
|
Chris@0
|
306 $reflector = new \ReflectionClass($this->minkDefaultDriverClass);
|
Chris@0
|
307 $driver = $reflector->newInstanceArgs($this->minkDefaultDriverArgs);
|
Chris@0
|
308 }
|
Chris@0
|
309 else {
|
Chris@0
|
310 $driver = new $this->minkDefaultDriverClass();
|
Chris@0
|
311 }
|
Chris@0
|
312 return $driver;
|
Chris@0
|
313 }
|
Chris@0
|
314
|
Chris@0
|
315 /**
|
Chris@14
|
316 * Get the Mink driver args from an environment variable, if it is set. Can
|
Chris@14
|
317 * be overridden in a derived class so it is possible to use a different
|
Chris@14
|
318 * value for a subset of tests, e.g. the JavaScript tests.
|
Chris@14
|
319 *
|
Chris@14
|
320 * @return string|false
|
Chris@14
|
321 * The JSON-encoded argument string. False if it is not set.
|
Chris@14
|
322 */
|
Chris@14
|
323 protected function getMinkDriverArgs() {
|
Chris@14
|
324 return getenv('MINK_DRIVER_ARGS');
|
Chris@14
|
325 }
|
Chris@14
|
326
|
Chris@14
|
327 /**
|
Chris@0
|
328 * Provides a Guzzle middleware handler to log every response received.
|
Chris@0
|
329 *
|
Chris@0
|
330 * @return callable
|
Chris@0
|
331 * The callable handler that will do the logging.
|
Chris@0
|
332 */
|
Chris@0
|
333 protected function getResponseLogHandler() {
|
Chris@0
|
334 return function (callable $handler) {
|
Chris@0
|
335 return function (RequestInterface $request, array $options) use ($handler) {
|
Chris@0
|
336 return $handler($request, $options)
|
Chris@0
|
337 ->then(function (ResponseInterface $response) use ($request) {
|
Chris@0
|
338 if ($this->htmlOutputEnabled) {
|
Chris@0
|
339
|
Chris@0
|
340 $caller = $this->getTestMethodCaller();
|
Chris@0
|
341 $html_output = 'Called from ' . $caller['function'] . ' line ' . $caller['line'];
|
Chris@0
|
342 $html_output .= '<hr />' . $request->getMethod() . ' request to: ' . $request->getUri();
|
Chris@0
|
343
|
Chris@0
|
344 // On redirect responses (status code starting with '3') we need
|
Chris@0
|
345 // to remove the meta tag that would do a browser refresh. We
|
Chris@0
|
346 // don't want to redirect developers away when they look at the
|
Chris@0
|
347 // debug output file in their browser.
|
Chris@0
|
348 $body = $response->getBody();
|
Chris@0
|
349 $status_code = (string) $response->getStatusCode();
|
Chris@0
|
350 if ($status_code[0] === '3') {
|
Chris@0
|
351 $body = preg_replace('#<meta http-equiv="refresh" content=.+/>#', '', $body, 1);
|
Chris@0
|
352 }
|
Chris@0
|
353 $html_output .= '<hr />' . $body;
|
Chris@0
|
354 $html_output .= $this->formatHtmlOutputHeaders($response->getHeaders());
|
Chris@0
|
355
|
Chris@0
|
356 $this->htmlOutput($html_output);
|
Chris@0
|
357 }
|
Chris@0
|
358 return $response;
|
Chris@0
|
359 });
|
Chris@0
|
360 };
|
Chris@0
|
361 };
|
Chris@0
|
362 }
|
Chris@0
|
363
|
Chris@0
|
364 /**
|
Chris@0
|
365 * Registers additional Mink sessions.
|
Chris@0
|
366 *
|
Chris@0
|
367 * Tests wishing to use a different driver or change the default driver should
|
Chris@0
|
368 * override this method.
|
Chris@0
|
369 *
|
Chris@0
|
370 * @code
|
Chris@0
|
371 * // Register a new session that uses the MinkPonyDriver.
|
Chris@0
|
372 * $pony = new MinkPonyDriver();
|
Chris@0
|
373 * $session = new Session($pony);
|
Chris@0
|
374 * $this->mink->registerSession('pony', $session);
|
Chris@0
|
375 * @endcode
|
Chris@0
|
376 */
|
Chris@0
|
377 protected function registerSessions() {}
|
Chris@0
|
378
|
Chris@0
|
379 /**
|
Chris@0
|
380 * {@inheritdoc}
|
Chris@0
|
381 */
|
Chris@0
|
382 protected function setUp() {
|
Chris@12
|
383 // Installing Drupal creates 1000s of objects. Garbage collection of these
|
Chris@12
|
384 // objects is expensive. This appears to be causing random segmentation
|
Chris@12
|
385 // faults in PHP 5.x due to https://bugs.php.net/bug.php?id=72286. Once
|
Chris@12
|
386 // Drupal is installed is rebuilt, garbage collection is re-enabled.
|
Chris@12
|
387 $disable_gc = version_compare(PHP_VERSION, '7', '<') && gc_enabled();
|
Chris@12
|
388 if ($disable_gc) {
|
Chris@12
|
389 gc_collect_cycles();
|
Chris@12
|
390 gc_disable();
|
Chris@12
|
391 }
|
Chris@0
|
392 parent::setUp();
|
Chris@0
|
393
|
Chris@0
|
394 $this->setupBaseUrl();
|
Chris@0
|
395
|
Chris@0
|
396 // Install Drupal test site.
|
Chris@0
|
397 $this->prepareEnvironment();
|
Chris@0
|
398 $this->installDrupal();
|
Chris@0
|
399
|
Chris@0
|
400 // Setup Mink.
|
Chris@17
|
401 $this->initMink();
|
Chris@0
|
402
|
Chris@0
|
403 // Set up the browser test output file.
|
Chris@0
|
404 $this->initBrowserOutputFile();
|
Chris@12
|
405 // If garbage collection was disabled prior to rebuilding container,
|
Chris@12
|
406 // re-enable it.
|
Chris@12
|
407 if ($disable_gc) {
|
Chris@12
|
408 gc_enable();
|
Chris@12
|
409 }
|
Chris@14
|
410
|
Chris@14
|
411 // Ensure that the test is not marked as risky because of no assertions. In
|
Chris@14
|
412 // PHPUnit 6 tests that only make assertions using $this->assertSession()
|
Chris@14
|
413 // can be marked as risky.
|
Chris@14
|
414 $this->addToAssertionCount(1);
|
Chris@0
|
415 }
|
Chris@0
|
416
|
Chris@0
|
417 /**
|
Chris@18
|
418 * Ensures test files are deletable.
|
Chris@0
|
419 *
|
Chris@0
|
420 * Some tests chmod generated files to be read only. During
|
Chris@0
|
421 * BrowserTestBase::cleanupEnvironment() and other cleanup operations,
|
Chris@0
|
422 * these files need to get deleted too.
|
Chris@0
|
423 *
|
Chris@0
|
424 * @param string $path
|
Chris@0
|
425 * The file path.
|
Chris@18
|
426 *
|
Chris@18
|
427 * @see \Drupal\Core\File\FileSystemInterface::deleteRecursive()
|
Chris@0
|
428 */
|
Chris@0
|
429 public static function filePreDeleteCallback($path) {
|
Chris@0
|
430 // When the webserver runs with the same system user as phpunit, we can
|
Chris@0
|
431 // make read-only files writable again. If not, chmod will fail while the
|
Chris@0
|
432 // file deletion still works if file permissions have been configured
|
Chris@0
|
433 // correctly. Thus, we ignore any problems while running chmod.
|
Chris@0
|
434 @chmod($path, 0700);
|
Chris@0
|
435 }
|
Chris@0
|
436
|
Chris@0
|
437 /**
|
Chris@0
|
438 * Clean up the Simpletest environment.
|
Chris@0
|
439 */
|
Chris@0
|
440 protected function cleanupEnvironment() {
|
Chris@0
|
441 // Remove all prefixed tables.
|
Chris@0
|
442 $original_connection_info = Database::getConnectionInfo('simpletest_original_default');
|
Chris@0
|
443 $original_prefix = $original_connection_info['default']['prefix']['default'];
|
Chris@0
|
444 $test_connection_info = Database::getConnectionInfo('default');
|
Chris@0
|
445 $test_prefix = $test_connection_info['default']['prefix']['default'];
|
Chris@0
|
446 if ($original_prefix != $test_prefix) {
|
Chris@0
|
447 $tables = Database::getConnection()->schema()->findTables('%');
|
Chris@0
|
448 foreach ($tables as $table) {
|
Chris@0
|
449 if (Database::getConnection()->schema()->dropTable($table)) {
|
Chris@0
|
450 unset($tables[$table]);
|
Chris@0
|
451 }
|
Chris@0
|
452 }
|
Chris@0
|
453 }
|
Chris@0
|
454
|
Chris@0
|
455 // Delete test site directory.
|
Chris@18
|
456 \Drupal::service('file_system')->deleteRecursive($this->siteDirectory, [$this, 'filePreDeleteCallback']);
|
Chris@0
|
457 }
|
Chris@0
|
458
|
Chris@0
|
459 /**
|
Chris@0
|
460 * {@inheritdoc}
|
Chris@0
|
461 */
|
Chris@0
|
462 protected function tearDown() {
|
Chris@0
|
463 parent::tearDown();
|
Chris@0
|
464
|
Chris@0
|
465 // Destroy the testing kernel.
|
Chris@0
|
466 if (isset($this->kernel)) {
|
Chris@0
|
467 $this->cleanupEnvironment();
|
Chris@0
|
468 $this->kernel->shutdown();
|
Chris@0
|
469 }
|
Chris@0
|
470
|
Chris@0
|
471 // Ensure that internal logged in variable is reset.
|
Chris@0
|
472 $this->loggedInUser = FALSE;
|
Chris@0
|
473
|
Chris@0
|
474 if ($this->mink) {
|
Chris@0
|
475 $this->mink->stopSessions();
|
Chris@0
|
476 }
|
Chris@0
|
477
|
Chris@0
|
478 // Restore original shutdown callbacks.
|
Chris@0
|
479 if (function_exists('drupal_register_shutdown_function')) {
|
Chris@0
|
480 $callbacks = &drupal_register_shutdown_function();
|
Chris@0
|
481 $callbacks = $this->originalShutdownCallbacks;
|
Chris@0
|
482 }
|
Chris@0
|
483 }
|
Chris@0
|
484
|
Chris@0
|
485 /**
|
Chris@0
|
486 * Returns Mink session.
|
Chris@0
|
487 *
|
Chris@0
|
488 * @param string $name
|
Chris@0
|
489 * (optional) Name of the session. Defaults to the active session.
|
Chris@0
|
490 *
|
Chris@0
|
491 * @return \Behat\Mink\Session
|
Chris@0
|
492 * The active Mink session object.
|
Chris@0
|
493 */
|
Chris@0
|
494 public function getSession($name = NULL) {
|
Chris@0
|
495 return $this->mink->getSession($name);
|
Chris@0
|
496 }
|
Chris@0
|
497
|
Chris@0
|
498 /**
|
Chris@17
|
499 * Get session cookies from current session.
|
Chris@17
|
500 *
|
Chris@17
|
501 * @return \GuzzleHttp\Cookie\CookieJar
|
Chris@17
|
502 * A cookie jar with the current session.
|
Chris@17
|
503 */
|
Chris@17
|
504 protected function getSessionCookies() {
|
Chris@17
|
505 $domain = parse_url($this->getUrl(), PHP_URL_HOST);
|
Chris@17
|
506 $session_id = $this->getSession()->getCookie($this->getSessionName());
|
Chris@17
|
507 $cookies = CookieJar::fromArray([$this->getSessionName() => $session_id], $domain);
|
Chris@17
|
508
|
Chris@17
|
509 return $cookies;
|
Chris@17
|
510 }
|
Chris@17
|
511
|
Chris@17
|
512 /**
|
Chris@16
|
513 * Obtain the HTTP client for the system under test.
|
Chris@16
|
514 *
|
Chris@16
|
515 * Use this method for arbitrary HTTP requests to the site under test. For
|
Chris@16
|
516 * most tests, you should not get the HTTP client and instead use navigation
|
Chris@16
|
517 * methods such as drupalGet() and clickLink() in order to benefit from
|
Chris@16
|
518 * assertions.
|
Chris@16
|
519 *
|
Chris@16
|
520 * Subclasses which substitute a different Mink driver should override this
|
Chris@16
|
521 * method and provide a Guzzle client if the Mink driver provides one.
|
Chris@16
|
522 *
|
Chris@16
|
523 * @return \GuzzleHttp\ClientInterface
|
Chris@16
|
524 * The client with BrowserTestBase configuration.
|
Chris@16
|
525 *
|
Chris@16
|
526 * @throws \RuntimeException
|
Chris@16
|
527 * If the Mink driver does not support a Guzzle HTTP client, throw an
|
Chris@16
|
528 * exception.
|
Chris@16
|
529 */
|
Chris@16
|
530 protected function getHttpClient() {
|
Chris@16
|
531 /* @var $mink_driver \Behat\Mink\Driver\DriverInterface */
|
Chris@16
|
532 $mink_driver = $this->getSession()->getDriver();
|
Chris@16
|
533 if ($mink_driver instanceof GoutteDriver) {
|
Chris@16
|
534 return $mink_driver->getClient()->getClient();
|
Chris@16
|
535 }
|
Chris@16
|
536 throw new \RuntimeException('The Mink client type ' . get_class($mink_driver) . ' does not support getHttpClient().');
|
Chris@16
|
537 }
|
Chris@16
|
538
|
Chris@16
|
539 /**
|
Chris@0
|
540 * Helper function to get the options of select field.
|
Chris@0
|
541 *
|
Chris@0
|
542 * @param \Behat\Mink\Element\NodeElement|string $select
|
Chris@0
|
543 * Name, ID, or Label of select field to assert.
|
Chris@0
|
544 * @param \Behat\Mink\Element\Element $container
|
Chris@0
|
545 * (optional) Container element to check against. Defaults to current page.
|
Chris@0
|
546 *
|
Chris@0
|
547 * @return array
|
Chris@0
|
548 * Associative array of option keys and values.
|
Chris@0
|
549 */
|
Chris@0
|
550 protected function getOptions($select, Element $container = NULL) {
|
Chris@0
|
551 if (is_string($select)) {
|
Chris@0
|
552 $select = $this->assertSession()->selectExists($select, $container);
|
Chris@0
|
553 }
|
Chris@0
|
554 $options = [];
|
Chris@0
|
555 /* @var \Behat\Mink\Element\NodeElement $option */
|
Chris@0
|
556 foreach ($select->findAll('xpath', '//option') as $option) {
|
Chris@0
|
557 $label = $option->getText();
|
Chris@0
|
558 $value = $option->getAttribute('value') ?: $label;
|
Chris@0
|
559 $options[$value] = $label;
|
Chris@0
|
560 }
|
Chris@0
|
561 return $options;
|
Chris@0
|
562 }
|
Chris@0
|
563
|
Chris@0
|
564 /**
|
Chris@0
|
565 * Installs Drupal into the Simpletest site.
|
Chris@0
|
566 */
|
Chris@0
|
567 public function installDrupal() {
|
Chris@0
|
568 $this->initUserSession();
|
Chris@0
|
569 $this->prepareSettings();
|
Chris@0
|
570 $this->doInstall();
|
Chris@0
|
571 $this->initSettings();
|
Chris@0
|
572 $container = $this->initKernel(\Drupal::request());
|
Chris@0
|
573 $this->initConfig($container);
|
Chris@0
|
574 $this->installModulesFromClassProperty($container);
|
Chris@0
|
575 $this->rebuildAll();
|
Chris@0
|
576 }
|
Chris@0
|
577
|
Chris@0
|
578 /**
|
Chris@0
|
579 * Prevents serializing any properties.
|
Chris@0
|
580 *
|
Chris@0
|
581 * Browser tests are run in a separate process. To do this PHPUnit creates a
|
Chris@0
|
582 * script to run the test. If it fails, the test result object will contain a
|
Chris@0
|
583 * stack trace which includes the test object. It will attempt to serialize
|
Chris@0
|
584 * it. Returning an empty array prevents it from serializing anything it
|
Chris@0
|
585 * should not.
|
Chris@0
|
586 *
|
Chris@0
|
587 * @return array
|
Chris@0
|
588 * An empty array.
|
Chris@0
|
589 *
|
Chris@0
|
590 * @see vendor/phpunit/phpunit/src/Util/PHP/Template/TestCaseMethod.tpl.dist
|
Chris@0
|
591 */
|
Chris@0
|
592 public function __sleep() {
|
Chris@0
|
593 return [];
|
Chris@0
|
594 }
|
Chris@0
|
595
|
Chris@0
|
596 /**
|
Chris@0
|
597 * Translates a CSS expression to its XPath equivalent.
|
Chris@0
|
598 *
|
Chris@0
|
599 * The search is relative to the root element (HTML tag normally) of the page.
|
Chris@0
|
600 *
|
Chris@0
|
601 * @param string $selector
|
Chris@0
|
602 * CSS selector to use in the search.
|
Chris@0
|
603 * @param bool $html
|
Chris@0
|
604 * (optional) Enables HTML support. Disable it for XML documents.
|
Chris@0
|
605 * @param string $prefix
|
Chris@0
|
606 * (optional) The prefix for the XPath expression.
|
Chris@0
|
607 *
|
Chris@0
|
608 * @return string
|
Chris@0
|
609 * The equivalent XPath of a CSS expression.
|
Chris@0
|
610 */
|
Chris@0
|
611 protected function cssSelectToXpath($selector, $html = TRUE, $prefix = 'descendant-or-self::') {
|
Chris@0
|
612 return (new CssSelectorConverter($html))->toXPath($selector, $prefix);
|
Chris@0
|
613 }
|
Chris@0
|
614
|
Chris@0
|
615 /**
|
Chris@0
|
616 * Performs an xpath search on the contents of the internal browser.
|
Chris@0
|
617 *
|
Chris@0
|
618 * The search is relative to the root element (HTML tag normally) of the page.
|
Chris@0
|
619 *
|
Chris@0
|
620 * @param string $xpath
|
Chris@0
|
621 * The xpath string to use in the search.
|
Chris@0
|
622 * @param array $arguments
|
Chris@0
|
623 * An array of arguments with keys in the form ':name' matching the
|
Chris@0
|
624 * placeholders in the query. The values may be either strings or numeric
|
Chris@0
|
625 * values.
|
Chris@0
|
626 *
|
Chris@0
|
627 * @return \Behat\Mink\Element\NodeElement[]
|
Chris@0
|
628 * The list of elements matching the xpath expression.
|
Chris@0
|
629 */
|
Chris@0
|
630 protected function xpath($xpath, array $arguments = []) {
|
Chris@0
|
631 $xpath = $this->assertSession()->buildXPathQuery($xpath, $arguments);
|
Chris@0
|
632 return $this->getSession()->getPage()->findAll('xpath', $xpath);
|
Chris@0
|
633 }
|
Chris@0
|
634
|
Chris@0
|
635 /**
|
Chris@0
|
636 * Configuration accessor for tests. Returns non-overridden configuration.
|
Chris@0
|
637 *
|
Chris@0
|
638 * @param string $name
|
Chris@0
|
639 * Configuration name.
|
Chris@0
|
640 *
|
Chris@0
|
641 * @return \Drupal\Core\Config\Config
|
Chris@0
|
642 * The configuration object with original configuration data.
|
Chris@0
|
643 */
|
Chris@0
|
644 protected function config($name) {
|
Chris@0
|
645 return $this->container->get('config.factory')->getEditable($name);
|
Chris@0
|
646 }
|
Chris@0
|
647
|
Chris@0
|
648 /**
|
Chris@0
|
649 * Returns all response headers.
|
Chris@0
|
650 *
|
Chris@0
|
651 * @return array
|
Chris@0
|
652 * The HTTP headers values.
|
Chris@0
|
653 *
|
Chris@0
|
654 * @deprecated Scheduled for removal in Drupal 9.0.0.
|
Chris@0
|
655 * Use $this->getSession()->getResponseHeaders() instead.
|
Chris@0
|
656 */
|
Chris@0
|
657 protected function drupalGetHeaders() {
|
Chris@0
|
658 return $this->getSession()->getResponseHeaders();
|
Chris@0
|
659 }
|
Chris@0
|
660
|
Chris@0
|
661 /**
|
Chris@0
|
662 * Gets the value of an HTTP response header.
|
Chris@0
|
663 *
|
Chris@0
|
664 * If multiple requests were required to retrieve the page, only the headers
|
Chris@0
|
665 * from the last request will be checked by default.
|
Chris@0
|
666 *
|
Chris@0
|
667 * @param string $name
|
Chris@0
|
668 * The name of the header to retrieve. Names are case-insensitive (see RFC
|
Chris@0
|
669 * 2616 section 4.2).
|
Chris@0
|
670 *
|
Chris@0
|
671 * @return string|null
|
Chris@0
|
672 * The HTTP header value or NULL if not found.
|
Chris@0
|
673 */
|
Chris@0
|
674 protected function drupalGetHeader($name) {
|
Chris@0
|
675 return $this->getSession()->getResponseHeader($name);
|
Chris@0
|
676 }
|
Chris@0
|
677
|
Chris@0
|
678 /**
|
Chris@0
|
679 * Gets the JavaScript drupalSettings variable for the currently-loaded page.
|
Chris@0
|
680 *
|
Chris@0
|
681 * @return array
|
Chris@0
|
682 * The JSON decoded drupalSettings value from the current page.
|
Chris@0
|
683 */
|
Chris@0
|
684 protected function getDrupalSettings() {
|
Chris@17
|
685 $html = $this->getSession()->getPage()->getContent();
|
Chris@0
|
686 if (preg_match('@<script type="application/json" data-drupal-selector="drupal-settings-json">([^<]*)</script>@', $html, $matches)) {
|
Chris@0
|
687 return Json::decode($matches[1]);
|
Chris@0
|
688 }
|
Chris@0
|
689 return [];
|
Chris@0
|
690 }
|
Chris@0
|
691
|
Chris@0
|
692 /**
|
Chris@0
|
693 * {@inheritdoc}
|
Chris@0
|
694 */
|
Chris@0
|
695 public static function assertEquals($expected, $actual, $message = '', $delta = 0.0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE) {
|
Chris@0
|
696 // Cast objects implementing MarkupInterface to string instead of
|
Chris@0
|
697 // relying on PHP casting them to string depending on what they are being
|
Chris@0
|
698 // comparing with.
|
Chris@0
|
699 $expected = static::castSafeStrings($expected);
|
Chris@0
|
700 $actual = static::castSafeStrings($actual);
|
Chris@0
|
701 parent::assertEquals($expected, $actual, $message, $delta, $maxDepth, $canonicalize, $ignoreCase);
|
Chris@0
|
702 }
|
Chris@0
|
703
|
Chris@0
|
704 /**
|
Chris@0
|
705 * Retrieves the current calling line in the class under test.
|
Chris@0
|
706 *
|
Chris@0
|
707 * @return array
|
Chris@0
|
708 * An associative array with keys 'file', 'line' and 'function'.
|
Chris@0
|
709 */
|
Chris@0
|
710 protected function getTestMethodCaller() {
|
Chris@0
|
711 $backtrace = debug_backtrace();
|
Chris@0
|
712 // Find the test class that has the test method.
|
Chris@0
|
713 while ($caller = Error::getLastCaller($backtrace)) {
|
Chris@0
|
714 if (isset($caller['class']) && $caller['class'] === get_class($this)) {
|
Chris@0
|
715 break;
|
Chris@0
|
716 }
|
Chris@0
|
717 // If the test method is implemented by a test class's parent then the
|
Chris@0
|
718 // class name of $this will not be part of the backtrace.
|
Chris@0
|
719 // In that case we process the backtrace until the caller is not a
|
Chris@0
|
720 // subclass of $this and return the previous caller.
|
Chris@0
|
721 if (isset($last_caller) && (!isset($caller['class']) || !is_subclass_of($this, $caller['class']))) {
|
Chris@0
|
722 // Return the last caller since that has to be the test class.
|
Chris@0
|
723 $caller = $last_caller;
|
Chris@0
|
724 break;
|
Chris@0
|
725 }
|
Chris@0
|
726 // Otherwise we have not reached our test class yet: save the last caller
|
Chris@0
|
727 // and remove an element from to backtrace to process the next call.
|
Chris@0
|
728 $last_caller = $caller;
|
Chris@0
|
729 array_shift($backtrace);
|
Chris@0
|
730 }
|
Chris@0
|
731
|
Chris@0
|
732 return $caller;
|
Chris@0
|
733 }
|
Chris@0
|
734
|
Chris@0
|
735 /**
|
Chris@16
|
736 * Transforms a nested array into a flat array suitable for drupalPostForm().
|
Chris@16
|
737 *
|
Chris@16
|
738 * @param array $values
|
Chris@16
|
739 * A multi-dimensional form values array to convert.
|
Chris@16
|
740 *
|
Chris@16
|
741 * @return array
|
Chris@16
|
742 * The flattened $edit array suitable for BrowserTestBase::drupalPostForm().
|
Chris@16
|
743 */
|
Chris@16
|
744 protected function translatePostValues(array $values) {
|
Chris@16
|
745 $edit = [];
|
Chris@16
|
746 // The easiest and most straightforward way to translate values suitable for
|
Chris@16
|
747 // BrowserTestBase::drupalPostForm() is to actually build the POST data
|
Chris@16
|
748 // string and convert the resulting key/value pairs back into a flat array.
|
Chris@16
|
749 $query = http_build_query($values);
|
Chris@16
|
750 foreach (explode('&', $query) as $item) {
|
Chris@16
|
751 list($key, $value) = explode('=', $item);
|
Chris@16
|
752 $edit[urldecode($key)] = urldecode($value);
|
Chris@16
|
753 }
|
Chris@16
|
754 return $edit;
|
Chris@16
|
755 }
|
Chris@16
|
756
|
Chris@0
|
757 }
|