BlockDependenciesTest.php 3.26 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\views\Tests\Plugin\BlockDependenciesTest.
 */

namespace Drupal\views\Tests\Plugin;

10
use Drupal\views\Tests\ViewKernelTestBase;
11 12

/**
13 14 15
 * Tests views block config dependencies functionality.
 *
 * @group views
16
 */
17
class BlockDependenciesTest extends ViewKernelTestBase {
18 19 20 21 22 23 24 25 26 27 28 29 30

  /**
   * Views used by this test.
   *
   * @var array
   */
  public static $testViews = array('test_exposed_block');

  /**
   * Modules to enable.
   *
   * @var array
   */
31
  public static $modules = array('node', 'block', 'user', 'field');
32 33 34 35 36 37 38 39 40 41

  /**
   * Tests that exposed filter blocks have the correct dependencies.
   *
   * @see \Drupal\views\Plugin\Derivative\ViewsExposedFilterBlock::getDerivativeDefinitions()
   */
  public function testExposedBlock() {
    $block = $this->createBlock('views_exposed_filter_block:test_exposed_block-page_1');
    $dependencies = $block->calculateDependencies();
    $expected = array(
42
      'config' => array('views.view.test_exposed_block'),
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
      'module' => array('views'),
      'theme' => array('stark')
    );
    $this->assertIdentical($expected, $dependencies);
  }

  /**
   * Tests that exposed filter blocks have the correct dependencies.
   *
   * @see \Drupal\views\Plugin\Derivative\ViewsBlock::getDerivativeDefinitions()
   */
  public function testViewsBlock() {
    $block = $this->createBlock('views_block:content_recent-block_1');
    $dependencies = $block->calculateDependencies();
    $expected = array(
58
      'config' => array('views.view.content_recent'),
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
      'module' => array('views'),
      'theme' => array('stark')
    );
    $this->assertIdentical($expected, $dependencies);
  }

  /**
   * Creates a block instance based on default settings.
   *
   * @param string $plugin_id
   *   The plugin ID of the block type for this block instance.
   * @param array $settings
   *   (optional) An associative array of settings for the block entity.
   *   Override the defaults by specifying the key and value in the array, for
   *   example:
   *   @code
   *     $this->createBlock('system_powered_by_block', array(
   *       'label' => t('Hello, world!'),
   *     ));
   *   @endcode
   *   The following defaults are provided:
   *   - label: Random string.
   *   - id: Random string.
   *   - region: 'sidebar_first'.
   *   - theme: The default theme.
   *   - visibility: Empty array.
   *
   * @return \Drupal\block\Entity\Block
   *   The block entity.
   */
  protected function createBlock($plugin_id, array $settings = array()) {
    $settings += array(
      'plugin' => $plugin_id,
      'region' => 'sidebar_first',
93
      'id' => strtolower($this->randomMachineName(8)),
94
      'theme' => $this->config('system.theme')->get('default'),
95
      'label' => $this->randomMachineName(8),
96 97 98
      'visibility' => array(),
      'weight' => 0,
    );
99 100
    $values = [];
    foreach (array('region', 'id', 'theme', 'plugin', 'weight', 'visibility') as $key) {
101 102 103 104
      $values[$key] = $settings[$key];
      // Remove extra values that do not belong in the settings array.
      unset($settings[$key]);
    }
105 106
    foreach ($values['visibility'] as $id => $visibility) {
      $values['visibility'][$id]['id'] = $id;
107
    }
108 109 110 111 112 113 114
    $values['settings'] = $settings;
    $block = entity_create('block', $values);
    $block->save();
    return $block;
  }

}