1a9f32eff7b2dd940949ad5a832496b669668be8
[yaffs-website] / web / core / modules / views / tests / src / Kernel / Plugin / BlockDependenciesTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Kernel\Plugin;
4
5 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
6 use Drupal\block\Entity\Block;
7
8 /**
9  * Tests views block config dependencies functionality.
10  *
11  * @group views
12  */
13 class BlockDependenciesTest extends ViewsKernelTestBase {
14
15   /**
16    * Views used by this test.
17    *
18    * @var array
19    */
20   public static $testViews = ['test_exposed_block'];
21
22   /**
23    * Modules to enable.
24    *
25    * @var array
26    */
27   public static $modules = ['node', 'block', 'user', 'field'];
28
29   /**
30    * Tests that exposed filter blocks have the correct dependencies.
31    *
32    * @see \Drupal\views\Plugin\Derivative\ViewsExposedFilterBlock::getDerivativeDefinitions()
33    */
34   public function testExposedBlock() {
35     $block = $this->createBlock('views_exposed_filter_block:test_exposed_block-page_1');
36     $dependencies = $block->calculateDependencies()->getDependencies();
37     $expected = [
38       'config' => ['views.view.test_exposed_block'],
39       'module' => ['views'],
40       'theme' => ['stark']
41     ];
42     $this->assertIdentical($expected, $dependencies);
43   }
44
45   /**
46    * Tests that exposed filter blocks have the correct dependencies.
47    *
48    * @see \Drupal\views\Plugin\Derivative\ViewsBlock::getDerivativeDefinitions()
49    */
50   public function testViewsBlock() {
51     $block = $this->createBlock('views_block:content_recent-block_1');
52     $dependencies = $block->calculateDependencies()->getDependencies();
53     $expected = [
54       'config' => ['views.view.content_recent'],
55       'module' => ['views'],
56       'theme' => ['stark']
57     ];
58     $this->assertIdentical($expected, $dependencies);
59   }
60
61   /**
62    * Creates a block instance based on default settings.
63    *
64    * @param string $plugin_id
65    *   The plugin ID of the block type for this block instance.
66    * @param array $settings
67    *   (optional) An associative array of settings for the block entity.
68    *   Override the defaults by specifying the key and value in the array, for
69    *   example:
70    *   @code
71    *     $this->createBlock('system_powered_by_block', array(
72    *       'label' => t('Hello, world!'),
73    *     ));
74    *   @endcode
75    *   The following defaults are provided:
76    *   - label: Random string.
77    *   - id: Random string.
78    *   - region: 'sidebar_first'.
79    *   - theme: The default theme.
80    *   - visibility: Empty array.
81    *
82    * @return \Drupal\block\Entity\Block
83    *   The block entity.
84    */
85   protected function createBlock($plugin_id, array $settings = []) {
86     $settings += [
87       'plugin' => $plugin_id,
88       'region' => 'sidebar_first',
89       'id' => strtolower($this->randomMachineName(8)),
90       'theme' => $this->config('system.theme')->get('default'),
91       'label' => $this->randomMachineName(8),
92       'visibility' => [],
93       'weight' => 0,
94     ];
95     $values = [];
96     foreach (['region', 'id', 'theme', 'plugin', 'weight', 'visibility'] as $key) {
97       $values[$key] = $settings[$key];
98       // Remove extra values that do not belong in the settings array.
99       unset($settings[$key]);
100     }
101     foreach ($values['visibility'] as $id => $visibility) {
102       $values['visibility'][$id]['id'] = $id;
103     }
104     $values['settings'] = $settings;
105     $block = Block::create($values);
106     $block->save();
107     return $block;
108   }
109
110 }