3 namespace Drupal\Tests\system\Functional\Common;
5 use Drupal\Tests\BrowserTestBase;
8 * Tests alteration of arguments passed to \Drupal::moduleHandler->alter().
12 class AlterTest extends BrowserTestBase {
19 public static $modules = ['block', 'common_test'];
22 * Tests if the theme has been altered.
24 public function testDrupalAlter() {
25 // This test depends on Bartik, so make sure that it is always the current
27 \Drupal::service('theme_handler')->install(['bartik']);
28 \Drupal::theme()->setActiveTheme(\Drupal::service('theme.initialization')->initTheme('bartik'));
30 $array = ['foo' => 'bar'];
31 $entity = new \stdClass();
34 // Verify alteration of a single argument.
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.');
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.');
48 // Verify alteration of multiple arguments.
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.');
62 // Verify alteration order when passing an array of types to \Drupal::moduleHandler->alter().
63 // common_test_module_implements_alter() places 'block' implementation after
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.');