BlockTemplateSuggestionsTest.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace Drupal\Tests\block\Functional;
  3. use Drupal\block\Entity\Block;
  4. use Drupal\Tests\BrowserTestBase;
  5. /**
  6. * Tests the block_theme_suggestions_block() function.
  7. *
  8. * @group block
  9. */
  10. class BlockTemplateSuggestionsTest extends BrowserTestBase {
  11. /**
  12. * Modules to install.
  13. *
  14. * @var array
  15. */
  16. public static $modules = ['block'];
  17. /**
  18. * Tests template suggestions from block_theme_suggestions_block().
  19. */
  20. public function testBlockThemeHookSuggestions() {
  21. // Define a block with a derivative to be preprocessed, which includes both
  22. // an underscore (not transformed) and a hyphen (transformed to underscore),
  23. // and generates possibilities for each level of derivative.
  24. // @todo Clarify this comment.
  25. $block = Block::create([
  26. 'plugin' => 'system_menu_block:admin',
  27. 'region' => 'footer',
  28. 'id' => 'machinename',
  29. ]);
  30. $variables = [];
  31. $plugin = $block->getPlugin();
  32. $variables['elements']['#configuration'] = $plugin->getConfiguration();
  33. $variables['elements']['#plugin_id'] = $plugin->getPluginId();
  34. $variables['elements']['#id'] = $block->id();
  35. $variables['elements']['#base_plugin_id'] = $plugin->getBaseId();
  36. $variables['elements']['#derivative_plugin_id'] = $plugin->getDerivativeId();
  37. $variables['elements']['content'] = [];
  38. $suggestions = block_theme_suggestions_block($variables);
  39. $this->assertEqual($suggestions, ['block__system', 'block__system_menu_block', 'block__system_menu_block__admin', 'block__machinename']);
  40. }
  41. }