AlterTest.php

You are here

  1. 8 core/modules/system/lib/Drupal/system/Tests/Database/AlterTest.php
  2. 8 core/modules/system/lib/Drupal/system/Tests/Form/AlterTest.php
  3. 8 core/modules/system/lib/Drupal/system/Tests/Common/AlterTest.php

Definition of Drupal\system\Tests\Common\AlterTest.

Namespace

Drupal\system\Tests\Common

Classes

Namesort descending Description
AlterTest Tests alteration of arguments passed to \Drupal::moduleHandler->alter().

File

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