Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / block_content / tests / src / Functional / BlockContentSaveTest.php
1 <?php
2
3 namespace Drupal\Tests\block_content\Functional;
4
5 use Drupal\block_content\Entity\BlockContent;
6
7 /**
8  * Tests $block_content->save() for saving content.
9  *
10  * @group block_content
11  */
12 class BlockContentSaveTest extends BlockContentTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['block_content_test'];
20
21   /**
22    * Sets the test up.
23    */
24   protected function setUp() {
25     parent::setUp();
26
27     $this->drupalLogin($this->adminUser);
28   }
29
30   /**
31    * Checks whether custom block IDs are saved properly during an import.
32    */
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);
38     $block_array = [
39       'info' => $info,
40       'body' => ['value' => $this->randomMachineName(32)],
41       'type' => 'basic',
42       'id' => $test_id
43     ];
44     $block = BlockContent::create($block_array);
45     $block->enforceIsNew(TRUE);
46     $block->save();
47
48     // Verify that block_submit did not wipe the provided id.
49     $this->assertEqual($block->id(), $test_id, 'Block imported using provide id');
50
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']);
55   }
56
57   /**
58    * Tests determining changes in hook_block_presave().
59    *
60    * Verifies the static block load cache is cleared upon save.
61    */
62   public function testDeterminingChanges() {
63     // Initial creation.
64     $block = $this->createBlockContent('test_changes');
65     $this->assertEqual($block->getChangedTime(), REQUEST_TIME, 'Creating a block sets default "changed" timestamp.');
66
67     // Update the block without applying changes.
68     $block->save();
69     $this->assertEqual($block->label(), 'test_changes', 'No changes have been determined.');
70
71     // Apply changes.
72     $block->setInfo('updated');
73     $block->save();
74
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.');
80
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.');
84   }
85
86   /**
87    * Tests saving a block on block insert.
88    *
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.
92    *
93    * @see block_test_block_insert()
94    */
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.');
100   }
101
102 }