comparison core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 1fec387a4317
children af1871eacc83
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
1 <?php 1 <?php
2 2
3 namespace Drupal\Core\Test; 3 namespace Drupal\Core\Test;
4 4
5 use Drupal\Component\FileCache\FileCacheFactory; 5 use Drupal\Component\FileCache\FileCacheFactory;
6 use Drupal\Component\Utility\SafeMarkup; 6 use Drupal\Component\Render\FormattableMarkup;
7 use Drupal\Core\Cache\Cache;
8 use Drupal\Core\Config\Development\ConfigSchemaChecker; 7 use Drupal\Core\Config\Development\ConfigSchemaChecker;
9 use Drupal\Core\Database\Database; 8 use Drupal\Core\Database\Database;
10 use Drupal\Core\DrupalKernel; 9 use Drupal\Core\DrupalKernel;
11 use Drupal\Core\Extension\MissingDependencyException; 10 use Drupal\Core\Extension\MissingDependencyException;
12 use Drupal\Core\Serialization\Yaml; 11 use Drupal\Core\Serialization\Yaml;
13 use Drupal\Core\Session\UserSession; 12 use Drupal\Core\Session\UserSession;
14 use Drupal\Core\Site\Settings; 13 use Drupal\Core\Site\Settings;
15 use Drupal\Core\StreamWrapper\StreamWrapperInterface; 14 use Drupal\Core\StreamWrapper\StreamWrapperInterface;
15 use Drupal\Tests\SessionTestTrait;
16 use Symfony\Component\DependencyInjection\ContainerInterface; 16 use Symfony\Component\DependencyInjection\ContainerInterface;
17 use Symfony\Component\HttpFoundation\Request; 17 use Symfony\Component\HttpFoundation\Request;
18 use Symfony\Component\Yaml\Yaml as SymfonyYaml; 18 use Symfony\Component\Yaml\Yaml as SymfonyYaml;
19 19
20 /** 20 /**
21 * Defines a trait for shared functional test setup functionality. 21 * Defines a trait for shared functional test setup functionality.
22 */ 22 */
23 trait FunctionalTestSetupTrait { 23 trait FunctionalTestSetupTrait {
24
25 use SessionTestTrait;
26 use RefreshVariablesTrait;
24 27
25 /** 28 /**
26 * The "#1" admin user. 29 * The "#1" admin user.
27 * 30 *
28 * @var \Drupal\Core\Session\AccountInterface 31 * @var \Drupal\Core\Session\AccountInterface
218 // Reset static variables and reload permissions. 221 // Reset static variables and reload permissions.
219 $this->refreshVariables(); 222 $this->refreshVariables();
220 } 223 }
221 224
222 /** 225 /**
223 * Refreshes in-memory configuration and state information.
224 *
225 * Useful after a page request is made that changes configuration or state in
226 * a different thread.
227 *
228 * In other words calling a settings page with $this->drupalPostForm() with a
229 * changed value would update configuration to reflect that change, but in the
230 * thread that made the call (thread running the test) the changed values
231 * would not be picked up.
232 *
233 * This method clears the cache and loads a fresh copy.
234 */
235 protected function refreshVariables() {
236 // Clear the tag cache.
237 \Drupal::service('cache_tags.invalidator')->resetChecksums();
238 foreach (Cache::getBins() as $backend) {
239 if (is_callable([$backend, 'reset'])) {
240 $backend->reset();
241 }
242 }
243
244 $this->container->get('config.factory')->reset();
245 $this->container->get('state')->resetCache();
246 }
247
248 /**
249 * Creates a mock request and sets it on the generator. 226 * Creates a mock request and sets it on the generator.
250 * 227 *
251 * This is used to manipulate how the generator generates paths during tests. 228 * This is used to manipulate how the generator generates paths during tests.
252 * It also ensures that calls to $this->drupalGet() will work when running 229 * It also ensures that calls to $this->drupalGet() will work when running
253 * from run-tests.sh because the url generator no longer looks at the global 230 * from run-tests.sh because the url generator no longer looks at the global
433 } 410 }
434 if ($modules) { 411 if ($modules) {
435 $modules = array_unique($modules); 412 $modules = array_unique($modules);
436 try { 413 try {
437 $success = $container->get('module_installer')->install($modules, TRUE); 414 $success = $container->get('module_installer')->install($modules, TRUE);
438 $this->assertTrue($success, SafeMarkup::format('Enabled modules: %modules', ['%modules' => implode(', ', $modules)])); 415 $this->assertTrue($success, new FormattableMarkup('Enabled modules: %modules', ['%modules' => implode(', ', $modules)]));
439 } 416 }
440 catch (MissingDependencyException $e) { 417 catch (MissingDependencyException $e) {
441 // The exception message has all the details. 418 // The exception message has all the details.
442 $this->fail($e->getMessage()); 419 $this->fail($e->getMessage());
443 } 420 }
660 * 637 *
661 * @return \Drupal\Core\Database\Install\Tasks[] 638 * @return \Drupal\Core\Database\Install\Tasks[]
662 * An array of available database driver installer objects. 639 * An array of available database driver installer objects.
663 */ 640 */
664 protected function getDatabaseTypes() { 641 protected function getDatabaseTypes() {
665 if ($this->originalContainer) { 642 if (isset($this->originalContainer) && $this->originalContainer) {
666 \Drupal::setContainer($this->originalContainer); 643 \Drupal::setContainer($this->originalContainer);
667 } 644 }
668 $database_types = drupal_get_database_types(); 645 $database_types = drupal_get_database_types();
669 if ($this->originalContainer) { 646 if (isset($this->originalContainer) && $this->originalContainer) {
670 \Drupal::unsetContainer(); 647 \Drupal::unsetContainer();
671 } 648 }
672 return $database_types; 649 return $database_types;
673 } 650 }
674 651