summaryrefslogtreecommitdiffstats
path: root/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockCreationTest.php
blob: 167014e9de09580d03b744c86dbba5af2602e7d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php

/**
 * @file
 * Contains \Drupal\custom_block\Tests\CustomBlockCreationTest.
 */

namespace Drupal\custom_block\Tests;

use Drupal\Core\Database\Database;

/**
 * Tests creating and saving a block.
 */
class CustomBlockCreationTest extends CustomBlockTestBase {

  /**
   * Modules to enable.
   *
   * Enable dummy module that implements hook_block_insert() for exceptions.
   *
   * @var array
   */
  public static $modules = array('custom_block_test', 'dblog');

  /**
   * Declares test information.
   */
  public static function getInfo() {
    return array(
      'name' => 'Custom Block creation',
      'description' => 'Create a block and test saving it.',
      'group' => 'Custom Block',
    );
  }

  /**
   * 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.
   */
  public function testCustomBlockCreation() {
    // Add a new view mode and verify if it is selected as expected.
    $this->drupalLogin($this->drupalCreateUser(array('administer display modes')));
    $this->drupalGet('admin/structure/display-modes/view/add/custom_block');
    $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);

    // Create a block.
    $edit = array();
    $edit['info[0][value]'] = 'Test Block';
    $edit['body[0][value]'] = $this->randomName(16);
    $this->drupalPostForm('block/add/basic', $edit, t('Save'));

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

    // Change the view mode.
    $view_mode['settings[custom_block][view_mode]'] = 'test_view_mode';
    $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');
    $this->assertFieldByXPath('//select[@name="settings[custom_block][view_mode]"]/option[@selected="selected"]/@value', 'test_view_mode', 'View mode changed to Test View Mode');

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

    // Check that the block exists in the database.
    $blocks = entity_load_multiple_by_properties('custom_block', array('info' => $edit['info[0][value]']));
    $block = reset($blocks);
    $this->assertTrue($block, 'Custom Block found in database.');

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

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

  /**
   * Create a default custom block.
   *
   * Creates a custom block from defaults and ensures that the 'basic block'
   * type is being used.
   */
  public function testDefaultCustomBlockCreation() {
    $edit = array();
    $edit['info[0][value]'] = $this->randomName(8);
    $edit['body[0][value]'] = $this->randomName(16);
    // Don't pass the custom block type in the url so the default is forced.
    $this->drupalPostForm('block/add', $edit, t('Save'));

    // 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',
      '%name' => $edit['info[0][value]'],
    )), 'Basic block created.');

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

  /**
   * Verifies that a transaction rolls back the failed creation.
   */
  public function testFailedBlockCreation() {
    // Create a block.
    try {
      $this->createCustomBlock('fail_creation');
      $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.
      $id = db_select('custom_block', 'b')
        ->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.
      $id = db_select('custom_block', 'b')
        ->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.');
    }
  }

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

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

    $block = custom_block_load(1);

    // 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.'));

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

    // Create another block and force the plugin cache to flush.
    $edit2 = array();
    $edit2['info[0][value]'] = $this->randomName(8);
    $body2 = $this->randomName(16);
    $edit2['body[0][value]'] = $body2;
    $this->drupalPostForm('block/add/basic', $edit2, t('Save'));

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

    // Create another block with no instances, and test we don't get a
    // confirmation message about deleting instances.
    $edit3 = array();
    $edit3['info[0][value]'] = $this->randomName(8);
    $body = $this->randomName(16);
    $edit3['body[0][value]'] = $body;
    $this->drupalPostForm('block/add/basic', $edit3, t('Save'));

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

}