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