class AlterTest

Same name in this branch
  1. 9 core/modules/system/tests/src/Functional/Form/AlterTest.php \Drupal\Tests\system\Functional\Form\AlterTest
  2. 9 core/tests/Drupal/KernelTests/Core/Database/AlterTest.php \Drupal\KernelTests\Core\Database\AlterTest
Same name and namespace in other branches
  1. 11.x core/modules/system/tests/src/Kernel/Common/AlterTest.php \Drupal\Tests\system\Kernel\Common\AlterTest
  2. 11.x core/modules/system/tests/src/Functional/Form/AlterTest.php \Drupal\Tests\system\Functional\Form\AlterTest
  3. 11.x core/tests/Drupal/KernelTests/Core/Database/AlterTest.php \Drupal\KernelTests\Core\Database\AlterTest
  4. 10 core/modules/system/tests/src/Kernel/Common/AlterTest.php \Drupal\Tests\system\Kernel\Common\AlterTest
  5. 10 core/modules/system/tests/src/Functional/Form/AlterTest.php \Drupal\Tests\system\Functional\Form\AlterTest
  6. 10 core/tests/Drupal/KernelTests/Core/Database/AlterTest.php \Drupal\KernelTests\Core\Database\AlterTest
  7. 8.9.x core/modules/system/tests/src/Functional/Form/AlterTest.php \Drupal\Tests\system\Functional\Form\AlterTest
  8. 8.9.x core/modules/system/tests/src/Functional/Common/AlterTest.php \Drupal\Tests\system\Functional\Common\AlterTest
  9. 8.9.x core/tests/Drupal/KernelTests/Core/Database/AlterTest.php \Drupal\KernelTests\Core\Database\AlterTest

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

@group Common

Hierarchy

Expanded class hierarchy of AlterTest

File

core/modules/system/tests/src/Functional/Common/AlterTest.php, line 12

Namespace

Drupal\Tests\system\Functional\Common
View source
class AlterTest extends BrowserTestBase {
  
  /**
   * Modules to enable.
   *
   * @var array
   */
  protected static $modules = [
    'block',
    'common_test',
  ];
  
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';
  
  /**
   * Tests if the theme has been altered.
   */
  public function testDrupalAlter() {
    // This test depends on Olivero, so make sure that it is always the current
    // active theme.
    \Drupal::service('theme_installer')->install([
      'olivero',
    ]);
    \Drupal::theme()->setActiveTheme(\Drupal::service('theme.initialization')->initTheme('olivero'));
    $array = [
      'foo' => 'bar',
    ];
    $entity = new \stdClass();
    $entity->foo = 'bar';
    // Verify alteration of a single argument.
    $array_copy = $array;
    $array_expected = [
      'foo' => 'Drupal theme',
    ];
    \Drupal::moduleHandler()->alter('drupal_alter', $array_copy);
    \Drupal::theme()->alter('drupal_alter', $array_copy);
    $this->assertEquals($array_expected, $array_copy, 'Single array was altered.');
    $entity_copy = clone $entity;
    $entity_expected = clone $entity;
    $entity_expected->foo = 'Drupal theme';
    \Drupal::moduleHandler()->alter('drupal_alter', $entity_copy);
    \Drupal::theme()->alter('drupal_alter', $entity_copy);
    $this->assertEquals($entity_expected, $entity_copy, 'Single object was altered.');
    // Verify alteration of multiple arguments.
    $array_copy = $array;
    $array_expected = [
      'foo' => 'Drupal theme',
    ];
    $entity_copy = clone $entity;
    $entity_expected = clone $entity;
    $entity_expected->foo = 'Drupal theme';
    $array2_copy = $array;
    $array2_expected = [
      'foo' => 'Drupal theme',
    ];
    \Drupal::moduleHandler()->alter('drupal_alter', $array_copy, $entity_copy, $array2_copy);
    \Drupal::theme()->alter('drupal_alter', $array_copy, $entity_copy, $array2_copy);
    $this->assertEquals($array_expected, $array_copy, 'First argument to \\Drupal::moduleHandler->alter() was altered.');
    $this->assertEquals($entity_expected, $entity_copy, 'Second argument to \\Drupal::moduleHandler->alter() was altered.');
    $this->assertEquals($array2_expected, $array2_copy, 'Third argument to \\Drupal::moduleHandler->alter() was altered.');
    // Verify alteration order when passing an array of types to \Drupal::moduleHandler->alter().
    // common_test_module_implements_alter() places 'block' implementation after
    // other modules.
    $array_copy = $array;
    $array_expected = [
      'foo' => 'Drupal block theme',
    ];
    \Drupal::moduleHandler()->alter([
      'drupal_alter',
      'drupal_alter_foo',
    ], $array_copy);
    \Drupal::theme()->alter([
      'drupal_alter',
      'drupal_alter_foo',
    ], $array_copy);
    $this->assertEquals($array_expected, $array_copy, 'hook_TYPE_alter() implementations ran in correct order.');
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.