3 namespace Drupal\Tests\block_content\Functional;
5 use Drupal\block_content\Entity\BlockContent;
8 * Tests $block_content->save() for saving content.
10 * @group block_content
12 class BlockContentSaveTest extends BlockContentTestBase {
19 public static $modules = ['block_content_test'];
24 protected function setUp() {
27 $this->drupalLogin($this->adminUser);
31 * Checks whether custom block IDs are saved properly during an import.
33 public function testImport() {
34 // Custom block ID must be a number that is not in the database.
35 $max_id = db_query('SELECT MAX(id) FROM {block_content}')->fetchField();
36 $test_id = $max_id + mt_rand(1000, 1000000);
37 $info = $this->randomMachineName(8);
40 'body' => ['value' => $this->randomMachineName(32)],
44 $block = BlockContent::create($block_array);
45 $block->enforceIsNew(TRUE);
48 // Verify that block_submit did not wipe the provided id.
49 $this->assertEqual($block->id(), $test_id, 'Block imported using provide id');
51 // Test the import saved.
52 $block_by_id = BlockContent::load($test_id);
53 $this->assertTrue($block_by_id, 'Custom block load by block ID.');
54 $this->assertIdentical($block_by_id->body->value, $block_array['body']['value']);
58 * Tests determining changes in hook_block_presave().
60 * Verifies the static block load cache is cleared upon save.
62 public function testDeterminingChanges() {
64 $block = $this->createBlockContent('test_changes');
65 $this->assertEqual($block->getChangedTime(), REQUEST_TIME, 'Creating a block sets default "changed" timestamp.');
67 // Update the block without applying changes.
69 $this->assertEqual($block->label(), 'test_changes', 'No changes have been determined.');
72 $block->setInfo('updated');
75 // The hook implementations block_content_test_block_content_presave() and
76 // block_content_test_block_content_update() determine changes and change
77 // the title as well as programmatically set the 'changed' timestamp.
78 $this->assertEqual($block->label(), 'updated_presave_update', 'Changes have been determined.');
79 $this->assertEqual($block->getChangedTime(), 979534800, 'Saving a custom block uses "changed" timestamp set in presave hook.');
81 // Test the static block load cache to be cleared.
82 $block = BlockContent::load($block->id());
83 $this->assertEqual($block->label(), 'updated_presave', 'Static cache has been cleared.');
87 * Tests saving a block on block insert.
89 * This test ensures that a block has been fully saved when
90 * hook_block_content_insert() is invoked, so that the block can be saved again
91 * in a hook implementation without errors.
93 * @see block_test_block_insert()
95 public function testBlockContentSaveOnInsert() {
96 // block_content_test_block_content_insert() triggers a save on insert if the
97 // title equals 'new'.
98 $block = $this->createBlockContent('new');
99 $this->assertEqual($block->label(), 'BlockContent ' . $block->id(), 'Custom block saved on block insert.');