Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / system / src / Tests / Common / AlterTest.php
1 <?php
2
3 namespace Drupal\system\Tests\Common;
4
5 use Drupal\simpletest\WebTestBase;
6
7 /**
8  * Tests alteration of arguments passed to \Drupal::moduleHandler->alter().
9  *
10  * @group Common
11  */
12 class AlterTest extends WebTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['block', 'common_test'];
20
21   /**
22    * Tests if the theme has been altered.
23    */
24   public function testDrupalAlter() {
25     // This test depends on Bartik, so make sure that it is always the current
26     // active theme.
27     \Drupal::service('theme_handler')->install(['bartik']);
28     \Drupal::theme()->setActiveTheme(\Drupal::service('theme.initialization')->initTheme('bartik'));
29
30     $array = ['foo' => 'bar'];
31     $entity = new \stdClass();
32     $entity->foo = 'bar';
33
34     // Verify alteration of a single argument.
35     $array_copy = $array;
36     $array_expected = ['foo' => 'Drupal theme'];
37     \Drupal::moduleHandler()->alter('drupal_alter', $array_copy);
38     \Drupal::theme()->alter('drupal_alter', $array_copy);
39     $this->assertEqual($array_copy, $array_expected, 'Single array was altered.');
40
41     $entity_copy = clone $entity;
42     $entity_expected = clone $entity;
43     $entity_expected->foo = 'Drupal theme';
44     \Drupal::moduleHandler()->alter('drupal_alter', $entity_copy);
45     \Drupal::theme()->alter('drupal_alter', $entity_copy);
46     $this->assertEqual($entity_copy, $entity_expected, 'Single object was altered.');
47
48     // Verify alteration of multiple arguments.
49     $array_copy = $array;
50     $array_expected = ['foo' => 'Drupal theme'];
51     $entity_copy = clone $entity;
52     $entity_expected = clone $entity;
53     $entity_expected->foo = 'Drupal theme';
54     $array2_copy = $array;
55     $array2_expected = ['foo' => 'Drupal theme'];
56     \Drupal::moduleHandler()->alter('drupal_alter', $array_copy, $entity_copy, $array2_copy);
57     \Drupal::theme()->alter('drupal_alter', $array_copy, $entity_copy, $array2_copy);
58     $this->assertEqual($array_copy, $array_expected, 'First argument to \Drupal::moduleHandler->alter() was altered.');
59     $this->assertEqual($entity_copy, $entity_expected, 'Second argument to \Drupal::moduleHandler->alter() was altered.');
60     $this->assertEqual($array2_copy, $array2_expected, 'Third argument to \Drupal::moduleHandler->alter() was altered.');
61
62     // Verify alteration order when passing an array of types to \Drupal::moduleHandler->alter().
63     // common_test_module_implements_alter() places 'block' implementation after
64     // other modules.
65     $array_copy = $array;
66     $array_expected = ['foo' => 'Drupal block theme'];
67     \Drupal::moduleHandler()->alter(['drupal_alter', 'drupal_alter_foo'], $array_copy);
68     \Drupal::theme()->alter(['drupal_alter', 'drupal_alter_foo'], $array_copy);
69     $this->assertEqual($array_copy, $array_expected, 'hook_TYPE_alter() implementations ran in correct order.');
70   }
71
72 }