BlockContentCreationTest.php 7.51 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\block_content\Tests\BlockContentCreationTest.
6 7
 */

8
namespace Drupal\block_content\Tests;
9 10

use Drupal\Core\Database\Database;
11
use Drupal\block_content\Entity\BlockContent;
12 13

/**
14 15 16
 * Create a block and test saving it.
 *
 * @group block_content
17
 */
18
class BlockContentCreationTest extends BlockContentTestBase {
19 20 21 22 23 24 25 26

  /**
   * Modules to enable.
   *
   * Enable dummy module that implements hook_block_insert() for exceptions.
   *
   * @var array
   */
27
  public static $modules = array('block_content_test', 'dblog', 'entity');
28 29 30 31 32 33 34 35 36 37 38 39

  /**
   * Sets the test up.
   */
  protected function setUp() {
    parent::setUp();
    $this->drupalLogin($this->adminUser);
  }

  /**
   * Creates a "Basic page" block and verifies its consistency in the database.
   */
40
  public function testBlockContentCreation() {
41 42
    // Add a new view mode and verify if it is selected as expected.
    $this->drupalLogin($this->drupalCreateUser(array('administer display modes')));
43
    $this->drupalGet('admin/structure/display-modes/view/add/block_content');
44 45 46 47 48 49 50 51 52
    $edit = array(
      'id' => 'test_view_mode',
      'label' => 'Test View Mode',
    );
    $this->drupalPostForm(NULL, $edit, t('Save'));
    $this->assertRaw(t('Saved the %label view mode.', array('%label' => $edit['label'])));

    $this->drupalLogin($this->adminUser);

53 54
    // Create a block.
    $edit = array();
55
    $edit['info[0][value]'] = 'Test Block';
56
    $edit['body[0][value]'] = $this->randomMachineName(16);
57
    $this->drupalPostForm('block/add/basic', $edit, t('Save'));
58 59 60 61

    // Check that the Basic block has been created.
    $this->assertRaw(format_string('!block %name has been created.', array(
      '!block' => 'Basic block',
62
      '%name' => $edit['info[0][value]']
63 64
    )), 'Basic block created.');

65
    // Change the view mode.
66
    $view_mode['settings[block_content][view_mode]'] = 'test_view_mode';
67 68 69 70
    $this->drupalPostForm(NULL, $view_mode, t('Save block'));

    // Go to the configure page and verify that the new view mode is correct.
    $this->drupalGet('admin/structure/block/manage/testblock');
71
    $this->assertFieldByXPath('//select[@name="settings[block_content][view_mode]"]/option[@selected="selected"]/@value', 'test_view_mode', 'View mode changed to Test View Mode');
72

73
    // Test the available view mode options.
74
    $this->assertOption('edit-settings-block-content-view-mode', 'default', 'The default view mode is available.');
75

76
    // Check that the block exists in the database.
77
    $blocks = entity_load_multiple_by_properties('block_content', array('info' => $edit['info[0][value]']));
78 79
    $block = reset($blocks);
    $this->assertTrue($block, 'Custom Block found in database.');
80 81 82

    // Check that attempting to create another block with the same value for
    // 'info' returns an error.
83
    $this->drupalPostForm('block/add/basic', $edit, t('Save'));
84 85 86

    // Check that the Basic block has been created.
    $this->assertRaw(format_string('A block with description %name already exists.', array(
87
      '%name' => $edit['info[0][value]']
88 89
    )));
    $this->assertResponse(200);
90 91
  }

92 93 94 95 96 97
  /**
   * Create a default custom block.
   *
   * Creates a custom block from defaults and ensures that the 'basic block'
   * type is being used.
   */
98
  public function testDefaultBlockContentCreation() {
99
    $edit = array();
100 101
    $edit['info[0][value]'] = $this->randomMachineName(8);
    $edit['body[0][value]'] = $this->randomMachineName(16);
102
    // Don't pass the custom block type in the url so the default is forced.
103
    $this->drupalPostForm('block/add', $edit, t('Save'));
104 105 106 107

    // Check that the block has been created and that it is a basic block.
    $this->assertRaw(format_string('!block %name has been created.', array(
      '!block' => 'Basic block',
108
      '%name' => $edit['info[0][value]'],
109 110 111
    )), 'Basic block created.');

    // Check that the block exists in the database.
112
    $blocks = entity_load_multiple_by_properties('block_content', array('info' => $edit['info[0][value]']));
113 114 115 116
    $block = reset($blocks);
    $this->assertTrue($block, 'Default Custom Block found in database.');
  }

117 118 119 120 121 122
  /**
   * Verifies that a transaction rolls back the failed creation.
   */
  public function testFailedBlockCreation() {
    // Create a block.
    try {
123
      $this->createBlockContent('fail_creation');
124 125 126 127 128 129 130 131
      $this->fail('Expected exception has not been thrown.');
    }
    catch (\Exception $e) {
      $this->pass('Expected exception has been thrown.');
    }

    if (Database::getConnection()->supportsTransactions()) {
      // Check that the block does not exist in the database.
132
      $id = db_select('block_content_field_data', 'b')
133 134 135 136 137 138 139 140
        ->fields('b', array('id'))
        ->condition('info', 'fail_creation')
        ->execute()
        ->fetchField();
      $this->assertFalse($id, 'Transactions supported, and block not found in database.');
    }
    else {
      // Check that the block exists in the database.
141
      $id = db_select('block_content_field_data', 'b')
142 143 144 145 146 147 148 149 150 151 152
        ->fields('b', array('id'))
        ->condition('info', 'fail_creation')
        ->execute()
        ->fetchField();
      $this->assertTrue($id, 'Transactions not supported, and block found in database.');

      // Check that the failed rollback was logged.
      $records = db_query("SELECT wid FROM {watchdog} WHERE message LIKE 'Explicit rollback failed%'")->fetchAll();
      $this->assertTrue(count($records) > 0, 'Transactions not supported, and rollback error logged to watchdog.');
    }
  }
153 154 155 156 157 158 159

  /**
   * Test deleting a block.
   */
  public function testBlockDelete() {
    // Create a block.
    $edit = array();
160 161
    $edit['info[0][value]'] = $this->randomMachineName(8);
    $body = $this->randomMachineName(16);
162
    $edit['body[0][value]'] = $body;
163
    $this->drupalPostForm('block/add/basic', $edit, t('Save'));
164 165 166

    // Place the block.
    $instance = array(
167 168
      'id' => drupal_strtolower($edit['info[0][value]']),
      'settings[label]' => $edit['info[0][value]'],
169 170
      'region' => 'sidebar_first',
    );
171 172
    $block = entity_load('block_content', 1);
    $url = 'admin/structure/block/add/block_content:' . $block->uuid() . '/' . \Drupal::config('system.theme')->get('default');
173
    $this->drupalPostForm($url, $instance, t('Save block'));
174

175
    $block = BlockContent::load(1);
176 177 178 179 180 181 182 183 184 185 186 187

    // Test getInstances method.
    $this->assertEqual(1, count($block->getInstances()));

    // Navigate to home page.
    $this->drupalGet('');
    $this->assertText($body);

    // Delete the block.
    $this->drupalGet('block/1/delete');
    $this->assertText(format_plural(1, 'This will also remove 1 placed block instance.', 'This will also remove @count placed block instance.'));

188
    $this->drupalPostForm(NULL, array(), 'Delete');
189
    $this->assertRaw(t('Custom block %name has been deleted.', array('%name' => $edit['info[0][value]'])));
190 191 192

    // Create another block and force the plugin cache to flush.
    $edit2 = array();
193 194
    $edit2['info[0][value]'] = $this->randomMachineName(8);
    $body2 = $this->randomMachineName(16);
195
    $edit2['body[0][value]'] = $body2;
196
    $this->drupalPostForm('block/add/basic', $edit2, t('Save'));
197 198 199 200 201 202

    $this->assertNoRaw('Error message');

    // Create another block with no instances, and test we don't get a
    // confirmation message about deleting instances.
    $edit3 = array();
203 204
    $edit3['info[0][value]'] = $this->randomMachineName(8);
    $body = $this->randomMachineName(16);
205
    $edit3['body[0][value]'] = $body;
206
    $this->drupalPostForm('block/add/basic', $edit3, t('Save'));
207 208 209 210 211 212

    // Show the delete confirm form.
    $this->drupalGet('block/3/delete');
    $this->assertNoText('This will also remove');
  }

213
}