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

/**
 * @file
 * Contains \Drupal\block\Tests\Views\DisplayBlockTest.
 */

namespace Drupal\block\Tests\Views;

10
use Drupal\Component\Utility\String;
11 12
use Drupal\views\Tests\ViewTestBase;
use Drupal\views\Tests\ViewTestData;
13
use Drupal\Core\Template\Attribute;
14 15 16 17 18 19 20 21 22 23 24 25 26

/**
 * Defines a test for block display.
 *
 * @see \Drupal\block\Plugin\views\display\Block
 */
class DisplayBlockTest extends ViewTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
27
  public static $modules = array('block_test_views', 'test_page_test', 'contextual', 'views_ui');
28 29 30 31 32 33 34 35 36 37 38 39

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

  public static function getInfo() {
    return array(
      'name' => ' Display: Block',
      'description' => 'Tests the block display plugin.',
40
      'group' => 'Views module integration',
41 42 43 44 45 46
    );
  }

  protected function setUp() {
    parent::setUp();

47
    ViewTestData::createTestViews(get_class($this), array('block_test_views'));
48 49 50
    $this->enableViewsTestModule();
  }

51 52 53 54 55 56 57 58 59 60 61 62 63 64
  /**
   * Tests default and custom block categories.
   */
  public function testBlockCategory() {
    $this->drupalLogin($this->drupalCreateUser(array('administer views', 'administer blocks')));

    // Create a new view in the UI.
    $edit = array();
    $edit['label'] = $this->randomString();
    $edit['id'] = strtolower($this->randomName());
    $edit['show[wizard_key]'] = 'standard:views_test_data';
    $edit['description'] = $this->randomString();
    $edit['block[create]'] = TRUE;
    $edit['block[style][row_plugin]'] = 'fields';
65
    $this->drupalPostForm('admin/structure/views/add', $edit, t('Save and edit'));
66 67 68 69

    // Test that the block was given a default category corresponding to its
    // base table.
    $arguments = array(
70
      ':id' => 'edit-category-lists-views',
71 72
      ':li_class' => 'views-block' . drupal_html_class($edit['id']) . '-block-1',
      ':href' => url('admin/structure/block/add/views_block:' . $edit['id'] . '-block_1/stark'),
73
      ':text' => $edit['label'],
74 75 76 77 78 79
    );
    $this->drupalGet('admin/structure/block');
    $elements = $this->xpath('//details[@id=:id]//li[contains(@class, :li_class)]/a[contains(@href, :href) and text()=:text]', $arguments);
    $this->assertTrue(!empty($elements), 'The test block appears in the category for its base table.');

    // Clone the block before changing the category.
80
    $this->drupalPostForm('admin/structure/views/view/' . $edit['id'] . '/edit/block_1', array(), t('Clone @display_title', array('@display_title' => 'Block')));
81 82 83 84
    $this->assertUrl('admin/structure/views/view/' . $edit['id'] . '/edit/block_2');

    // Change the block category to a random string.
    $this->drupalGet('admin/structure/views/view/' . $edit['id'] . '/edit/block_1');
85
    $label = t('Lists (Views)');
86 87 88 89
    $link = $this->xpath('//a[@id="views-block-1-block-category" and normalize-space(text())=:label]', array(':label' => $label));
    $this->assertTrue(!empty($link));
    $this->clickLink($label);
    $category = $this->randomString();
90
    $this->drupalPostForm(NULL, array('block_category' => $category), t('Apply'));
91 92

    // Clone the block after changing the category.
93
    $this->drupalPostForm(NULL, array(), t('Clone @display_title', array('@display_title' => 'Block')));
94 95
    $this->assertUrl('admin/structure/views/view/' . $edit['id'] . '/edit/block_3');

96
    $this->drupalPostForm(NULL, array(), t('Save'));
97 98

    // Test that the blocks are listed under the correct categories.
99
    $category_id = drupal_html_id('edit-category-' . String::checkPlain($category));
100 101 102 103 104 105
    $arguments[':id'] = $category_id;
    $this->drupalGet('admin/structure/block');
    $elements = $this->xpath('//details[@id=:id]//li[contains(@class, :li_class)]/a[contains(@href, :href) and text()=:text]', $arguments);
    $this->assertTrue(!empty($elements), 'The test block appears in the custom category.');

    $arguments = array(
106
      ':id' => 'edit-category-lists-views',
107 108
      ':li_class' => 'views-block' . drupal_html_class($edit['id']) . '-block-2',
      ':href' => url('admin/structure/block/add/views_block:' . $edit['id'] . '-block_2/stark'),
109
      ':text' => $edit['label'],
110 111 112 113 114 115 116 117
    );
    $elements = $this->xpath('//details[@id=:id]//li[contains(@class, :li_class)]/a[contains(@href, :href) and text()=:text]', $arguments);
    $this->assertTrue(!empty($elements), 'The first cloned test block remains in the original category.');

    $arguments = array(
      ':id' => $category_id,
      ':li_class' => 'views-block' . drupal_html_class($edit['id']) . '-block-3',
      ':href' => url('admin/structure/block/add/views_block:' . $edit['id'] . '-block_3/stark'),
118
      ':text' => $edit['label'],
119 120 121 122 123
    );
    $elements = $this->xpath('//details[@id=:id]//li[contains(@class, :li_class)]/a[contains(@href, :href) and text()=:text]', $arguments);
    $this->assertTrue(!empty($elements), 'The second cloned test block appears in the custom category.');
  }

124 125 126 127 128 129
  /**
   * Tests removing a block display.
   */
  protected function testDeleteBlockDisplay() {
    // To test all combinations possible we first place create two instances
    // of the block display of the first view.
130 131
    $block_1 = $this->drupalPlaceBlock('views_block:test_view_block-block_1', array('title' => 'test_view_block-block_1:1'));
    $block_2 = $this->drupalPlaceBlock('views_block:test_view_block-block_1', array('title' => 'test_view_block-block_1:2'));
132 133 134

    // Then we add one instance of blocks for each of the two displays of the
    // second view.
135 136
    $block_3 = $this->drupalPlaceBlock('views_block:test_view_block2-block_1', array('title' => 'test_view_block2-block_1'));
    $block_4 = $this->drupalPlaceBlock('views_block:test_view_block2-block_2', array('title' => 'test_view_block2-block_2'));
137 138 139 140 141 142 143

    $this->drupalGet('test-page');
    $this->assertBlockAppears($block_1);
    $this->assertBlockAppears($block_2);
    $this->assertBlockAppears($block_3);
    $this->assertBlockAppears($block_4);

144
    $block_storage_controller = $this->container->get('entity.manager')->getStorageController('block');
145 146

    // Remove the block display, so both block entities from the first view
147
    // should both disappear.
148 149 150 151 152
    $view = views_get_view('test_view_block');
    $view->initDisplay();
    $view->displayHandlers->remove('block_1');
    $view->storage->save();

153 154 155 156
    $this->assertFalse($block_storage_controller->load($block_1->id()), 'The block for this display was removed.');
    $this->assertFalse($block_storage_controller->load($block_2->id()), 'The block for this display was removed.');
    $this->assertTrue($block_storage_controller->load($block_3->id()), 'A block from another view was unaffected.');
    $this->assertTrue($block_storage_controller->load($block_4->id()), 'A block from another view was unaffected.');
157 158 159 160 161 162 163 164 165 166 167 168 169
    $this->drupalGet('test-page');
    $this->assertNoBlockAppears($block_1);
    $this->assertNoBlockAppears($block_2);
    $this->assertBlockAppears($block_3);
    $this->assertBlockAppears($block_4);

    // Remove the first block display of the second view and ensure the block
    // instance of the second block display still exists.
    $view = views_get_view('test_view_block2');
    $view->initDisplay();
    $view->displayHandlers->remove('block_1');
    $view->storage->save();

170 171
    $this->assertFalse($block_storage_controller->load($block_3->id()), 'The block for this display was removed.');
    $this->assertTrue($block_storage_controller->load($block_4->id()), 'A block from another display on the same view was unaffected.');
172 173 174 175 176
    $this->drupalGet('test-page');
    $this->assertNoBlockAppears($block_3);
    $this->assertBlockAppears($block_4);
  }

177 178 179 180 181
  /**
   * Test the block form for a Views block.
   */
  public function testViewsBlockForm() {
    $this->drupalLogin($this->drupalCreateUser(array('administer blocks')));
182
    $default_theme = \Drupal::config('system.theme')->get('default');
183 184 185
    $this->drupalGet('admin/structure/block/add/views_block:test_view_block-block_1/' . $default_theme);
    $elements = $this->xpath('//input[@name="label"]');
    $this->assertTrue(empty($elements), 'The label field is not found for Views blocks.');
186 187
    // Test that that machine name field is hidden from display and has been
    // saved as expected from the default value.
188
    $this->assertNoFieldById('edit-machine-name', 'views_block__test_view_block_1', 'The machine name is hidden on the views block form.');
189
    // Save the block.
190
    $this->drupalPostForm(NULL, array(), t('Save block'));
191
    $storage = $this->container->get('entity.manager')->getStorageController('block');
192
    $block = $storage->load('views_block__test_view_block_block_1');
193 194
    // This will only return a result if our new block has been created with the
    // expected machine name.
195
    $this->assertTrue(!empty($block), 'The expected block was loaded.');
196 197 198

    for ($i = 2; $i <= 3; $i++) {
      // Place the same block again and make sure we have a new ID.
199
      $this->drupalPostForm('admin/structure/block/add/views_block:test_view_block-block_1/' . $default_theme, array(), t('Save block'));
200
      $block = $storage->load('views_block__test_view_block_block_1_' . $i);
201 202
      // This will only return a result if our new block has been created with the
      // expected machine name.
203
      $this->assertTrue(!empty($block), 'The expected block was loaded.');
204
    }
205 206 207 208 209 210

    // Tests the override capability of items per page.
    $this->drupalGet('admin/structure/block/add/views_block:test_view_block-block_1/' . $default_theme);
    $edit = array();
    $edit['settings[override][items_per_page]'] = 10;

211
    $this->drupalPostForm('admin/structure/block/add/views_block:test_view_block-block_1/' . $default_theme, $edit, t('Save block'));
212

213
    $block = $storage->load('views_block__test_view_block_block_1_4');
214
    $config = $block->getPlugin()->getConfiguration();
215 216 217
    $this->assertEqual(10, $config['items_per_page'], "'Items per page' is properly saved.");

    $edit['settings[override][items_per_page]'] = 5;
218
    $this->drupalPostForm('admin/structure/block/manage/views_block__test_view_block_block_1_4', $edit, t('Save block'));
219

220
    $block = $storage->load('views_block__test_view_block_block_1_4');
221

222
    $config = $block->getPlugin()->getConfiguration();
223
    $this->assertEqual(5, $config['items_per_page'], "'Items per page' is properly saved.");
224 225
  }

226 227 228 229
  /**
   * Tests the contextual links on a Views block.
   */
  public function testBlockContextualLinks() {
230 231
    $this->drupalLogin($this->drupalCreateUser(array('administer views', 'access contextual links', 'administer blocks')));
    $block = $this->drupalPlaceBlock('views_block:test_view_block-block_1');
232
    $this->drupalGet('test-page');
233

234
    $id = 'block:block=' . $block->id() . ':|views_ui_edit:view=test_view_block:location=block&name=test_view_block&display_id=block_1';
235
    // @see \Drupal\contextual\Tests\ContextualDynamicContextTest:assertContextualLinkPlaceHolder()
236
    $this->assertRaw('<div' . new Attribute(array('data-contextual-id' => $id)) . '></div>', format_string('Contextual link placeholder with id @id exists.', array('@id' => $id)));
237 238 239

    // Get server-rendered contextual links.
    // @see \Drupal\contextual\Tests\ContextualDynamicContextTest:renderContextualLinks()
240 241
    $post = array('ids[0]' => $id);
    $response = $this->drupalPost('contextual/render', 'application/json', $post, array('query' => array('destination' => 'test-page')));
242 243
    $this->assertResponse(200);
    $json = drupal_json_decode($response);
244
    $this->assertIdentical($json[$id], '<ul class="contextual-links"><li class="block-configure odd first"><a href="' . base_path() . 'admin/structure/block/manage/' . $block->id() . '?destination=test-page">Configure block</a></li><li class="views-uiedit even last"><a href="' . base_path() . 'admin/structure/views/view/test_view_block/edit/block_1?destination=test-page">Edit view</a></li></ul>');
245 246
  }

247
}