/**
  * Checks block revision related operations.
  */
 public function testRevisions()
 {
     $blocks = $this->blocks;
     $logs = $this->revisionLogs;
     foreach ($blocks as $delta => $revision_id) {
         // Confirm the correct revision text appears.
         $loaded = entity_revision_load('block_content', $revision_id);
         // Verify revision log is the same.
         $this->assertEqual($loaded->getRevisionLog(), $logs[$delta], format_string('Correct log message found for revision !revision', array('!revision' => $loaded->getRevisionId())));
     }
     // Confirm that this is the default revision.
     $this->assertTrue($loaded->isDefaultRevision(), 'Third block revision is the default one.');
     // Make a new revision and set it to not be default.
     // This will create a new revision that is not "front facing".
     // Save this as a non-default revision.
     $loaded->setNewRevision();
     $loaded->isDefaultRevision(FALSE);
     $loaded->body = $this->randomMachineName(8);
     $loaded->save();
     $this->drupalGet('block/' . $loaded->id());
     $this->assertNoText($loaded->body->value, 'Revision body text is not present on default version of block.');
     // Verify that the non-default revision id is greater than the default
     // revision id.
     $default_revision = BlockContent::load($loaded->id());
     $this->assertTrue($loaded->getRevisionId() > $default_revision->getRevisionId(), 'Revision id is greater than default revision id.');
 }
 /**
  * Test for autocomplete processing.
  *
  * Tests that processing does not crash when the entity types of the
  * referenced entity and of the entity the field is attached to are different.
  */
 public function testEntityReferenceRevisionsAutocompleteProcessing()
 {
     $admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer nodes', 'administer blocks', 'create article content', 'administer content types', 'administer node fields', 'administer node display', 'administer node form display', 'edit any article content'));
     $this->drupalLogin($admin_user);
     // Create a custom block content bundle.
     $this->createBlockContentType(array('type' => 'customblockcontent', 'name' => 'Custom Block Content'));
     // Create entity reference revisions field attached to article.
     static::fieldUIAddNewField('admin/structure/types/manage/article', 'entity_reference_revisions', 'Entity reference revisions', 'entity_reference_revisions', array('settings[target_type]' => 'block_content', 'cardinality' => '-1'), array('settings[handler_settings][target_bundles][customblockcontent]' => TRUE));
     // Create custom block.
     $block_label = $this->randomMachineName();
     $block_content = $this->randomString();
     $edit = array('info[0][value]' => $block_label, 'body[0][value]' => $block_content, 'revision' => TRUE);
     $this->drupalPostForm('block/add', $edit, t('Save'));
     $block = $this->drupalGetBlockByInfo($block_label);
     // Create an article.
     $title = $this->randomMachineName();
     $edit = array('title[0][value]' => $title, 'body[0][value]' => 'Revision 1', 'field_entity_reference_revisions[0][target_id]' => $block_label . ' (' . $block->id() . ')', 'revision' => TRUE);
     $this->drupalPostForm('node/add/article', $edit, t('Save and publish'));
     $this->assertText($title);
     $this->assertText(Html::escape($block_content));
     // Check if the block content is not deleted since there is no composite
     // relationship.
     $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
     $node = Node::load($node->id());
     $node->delete();
     $this->assertNotNull(BlockContent::load($block->id()));
 }
 /**
  * Tests moderating custom blocks.
  *
  * Blocks and any non-node-type-entities do not have a concept of
  * "published". As such, we must use the "default revision" to know what is
  * going to be "published", i.e. visible to the user.
  *
  * The one exception is a block that has never been "published". When a block
  * is first created, it becomes the "default revision". For each edit of the
  * block after that, Content Moderation checks the "default revision" to
  * see if it is set to a published moderation state. If it is not, the entity
  * being saved will become the "default revision".
  *
  * The test below is intended, in part, to make this behavior clear.
  *
  * @see \Drupal\content_moderation\EntityOperations::entityPresave
  * @see \Drupal\content_moderation\Tests\ModerationFormTest::testModerationForm
  */
 public function testCustomBlockModeration()
 {
     $this->drupalLogin($this->rootUser);
     $this->drupalGet('admin/structure/block/block-content/types');
     $this->assertLinkByHref('admin/structure/block/block-content/manage/basic/moderation');
     $this->drupalGet('admin/structure/block/block-content/manage/basic');
     $this->assertLinkByHref('admin/structure/block/block-content/manage/basic/moderation');
     $this->drupalGet('admin/structure/block/block-content/manage/basic/moderation');
     // Enable moderation for custom blocks at
     // admin/structure/block/block-content/manage/basic/moderation.
     $edit = ['enable_moderation_state' => TRUE, 'allowed_moderation_states_unpublished[draft]' => TRUE, 'allowed_moderation_states_published[published]' => TRUE, 'default_moderation_state' => 'draft'];
     $this->drupalPostForm(NULL, $edit, t('Save'));
     $this->assertText(t('Your settings have been saved.'));
     // Create a custom block at block/add and save it as draft.
     $body = 'Body of moderated block';
     $edit = ['info[0][value]' => 'Moderated block', 'body[0][value]' => $body];
     $this->drupalPostForm('block/add', $edit, t('Save and Create New Draft'));
     $this->assertText(t('basic Moderated block has been created.'));
     // Place the block in the Sidebar First region.
     $instance = array('id' => 'moderated_block', 'settings[label]' => $edit['info[0][value]'], 'region' => 'sidebar_first');
     $block = BlockContent::load(1);
     $url = 'admin/structure/block/add/block_content:' . $block->uuid() . '/' . $this->config('system.theme')->get('default');
     $this->drupalPostForm($url, $instance, t('Save block'));
     // Navigate to home page and check that the block is visible. It should be
     // visible because it is the default revision.
     $this->drupalGet('');
     $this->assertText($body);
     // Update the block.
     $updated_body = 'This is the new body value';
     $edit = ['body[0][value]' => $updated_body];
     $this->drupalPostForm('block/' . $block->id(), $edit, t('Save and Create New Draft'));
     $this->assertText(t('basic Moderated block has been updated.'));
     // Navigate to the home page and check that the block shows the updated
     // content. It should show the updated content because the block's default
     // revision is not a published moderation state.
     $this->drupalGet('');
     $this->assertText($updated_body);
     // Publish the block so we can create a forward revision.
     $this->drupalPostForm('block/' . $block->id(), [], t('Save and Publish'));
     // Create a forward revision.
     $forward_revision_body = 'This is the forward revision body value';
     $edit = ['body[0][value]' => $forward_revision_body];
     $this->drupalPostForm('block/' . $block->id(), $edit, t('Save and Create New Draft'));
     $this->assertText(t('basic Moderated block has been updated.'));
     // Navigate to home page and check that the forward revision doesn't show,
     // since it should not be set as the default revision.
     $this->drupalGet('');
     $this->assertText($updated_body);
     // Open the latest tab and publish the new draft.
     $edit = ['new_state' => 'published'];
     $this->drupalPostForm('block/' . $block->id() . '/latest', $edit, t('Apply'));
     $this->assertText(t('The moderation state has been updated.'));
     // Navigate to home page and check that the forward revision is now the
     // default revision and therefore visible.
     $this->drupalGet('');
     $this->assertText($forward_revision_body);
 }
예제 #4
0
 /**
  * Tests migration of custom blocks from Drupal 7 to Drupal 8.
  */
 public function testCustomBlockMigration()
 {
     $block = BlockContent::load(1);
     $this->assertTrue($block instanceof BlockContentInterface);
     /** @var \Drupal\block_content\BlockContentInterface $block */
     $this->assertIdentical('Limerick', $block->label());
     $expected_body = "A fellow jumped off a high wall\r\nAnd had a most terrible fall\r\nHe went back to bed\r\nWith a bump on his head\r\nThat's why you don't jump off a wall";
     $this->assertIdentical($expected_body, $block->body->value);
     $this->assertIdentical('filtered_html', $block->body->format);
 }
 /**
  * Tests the Drupal 6 custom block to Drupal 8 migration.
  */
 public function testBlockMigration()
 {
     /** @var BlockContent $block */
     $block = BlockContent::load(1);
     $this->assertIdentical('My block 1', $block->label());
     $this->assertTrue(REQUEST_TIME <= $block->getChangedTime() && $block->getChangedTime() <= time());
     $this->assertIdentical('en', $block->language()->getId());
     $this->assertIdentical('<h3>My first custom block body</h3>', $block->body->value);
     $this->assertIdentical('full_html', $block->body->format);
     $block = BlockContent::load(2);
     $this->assertIdentical('My block 2', $block->label());
     $this->assertTrue(REQUEST_TIME <= $block->getChangedTime() && $block->getChangedTime() <= time());
     $this->assertIdentical('en', $block->language()->getId());
     $this->assertIdentical('<h3>My second custom block body</h3>', $block->body->value);
     $this->assertIdentical('full_html', $block->body->format);
 }
 /**
  * Tests determing changes in hook_block_presave().
  *
  * Verifies the static block load cache is cleared upon save.
  */
 public function testDeterminingChanges()
 {
     // Initial creation.
     $block = $this->createBlockContent('test_changes');
     $this->assertEqual($block->getChangedTime(), REQUEST_TIME, 'Creating a block sets default "changed" timestamp.');
     // Update the block without applying changes.
     $block->save();
     $this->assertEqual($block->label(), 'test_changes', 'No changes have been determined.');
     // Apply changes.
     $block->setInfo('updated');
     $block->save();
     // The hook implementations block_content_test_block_content_presave() and
     // block_content_test_block_content_update() determine changes and change the
     // title as well as programatically set the 'changed' timestamp.
     $this->assertEqual($block->label(), 'updated_presave_update', 'Changes have been determined.');
     $this->assertEqual($block->getChangedTime(), 979534800, 'Saving a custom block uses "changed" timestamp set in presave hook.');
     // Test the static block load cache to be cleared.
     $block = BlockContent::load($block->id());
     $this->assertEqual($block->label(), 'updated_presave', 'Static cache has been cleared.');
 }
예제 #7
0
 /**
  * Checks block edit functionality.
  */
 public function testPageEdit()
 {
     $this->drupalLogin($this->adminUser);
     $title_key = 'info[0][value]';
     $body_key = 'body[0][value]';
     // Create block to edit.
     $edit = array();
     $edit['info[0][value]'] = Unicode::strtolower($this->randomMachineName(8));
     $edit[$body_key] = $this->randomMachineName(16);
     $this->drupalPostForm('block/add/basic', $edit, t('Save'));
     // Check that the block exists in the database.
     $blocks = \Drupal::entityQuery('block_content')->condition('info', $edit['info[0][value]'])->execute();
     $block = BlockContent::load(reset($blocks));
     $this->assertTrue($block, 'Custom block found in database.');
     // Load the edit page.
     $this->drupalGet('block/' . $block->id());
     $this->assertFieldByName($title_key, $edit[$title_key], 'Title field displayed.');
     $this->assertFieldByName($body_key, $edit[$body_key], 'Body field displayed.');
     // Edit the content of the block.
     $edit = array();
     $edit[$title_key] = $this->randomMachineName(8);
     $edit[$body_key] = $this->randomMachineName(16);
     // Stay on the current page, without reloading.
     $this->drupalPostForm(NULL, $edit, t('Save'));
     // Edit the same block, creating a new revision.
     $this->drupalGet("block/" . $block->id());
     $edit = array();
     $edit['info[0][value]'] = $this->randomMachineName(8);
     $edit[$body_key] = $this->randomMachineName(16);
     $edit['revision'] = TRUE;
     $this->drupalPostForm(NULL, $edit, t('Save'));
     // Ensure that the block revision has been created.
     \Drupal::entityManager()->getStorage('block_content')->resetCache(array($block->id()));
     $revised_block = BlockContent::load($block->id());
     $this->assertNotIdentical($block->getRevisionId(), $revised_block->getRevisionId(), 'A new revision has been created.');
     // Test deleting the block.
     $this->drupalGet("block/" . $revised_block->id());
     $this->clickLink(t('Delete'));
     $this->assertText(format_string('Are you sure you want to delete the custom block @label?', array('@label' => $revised_block->label())));
 }
예제 #8
0
 /**
  * Test that placed content blocks create a dependency in the block placement.
  */
 public function testConfigDependencies()
 {
     $block = $this->createBlockContent();
     // Place the block.
     $block_placement_id = Unicode::strtolower($block->label());
     $instance = array('id' => $block_placement_id, 'settings[label]' => $block->label(), 'region' => 'sidebar_first');
     $block = BlockContent::load(1);
     $url = 'admin/structure/block/add/block_content:' . $block->uuid() . '/' . $this->config('system.theme')->get('default');
     $this->drupalPostForm($url, $instance, t('Save block'));
     $dependencies = \Drupal::service('config.manager')->findConfigEntityDependentsAsEntities('content', array($block->getConfigDependencyName()));
     $block_placement = reset($dependencies);
     $this->assertEqual($block_placement_id, $block_placement->id(), "The block placement config entity has a dependency on the block content entity.");
 }
예제 #9
0
 /**
  * Tests the block migration.
  */
 public function testBlockMigration()
 {
     $this->assertEntity('bartik_system_main', 'system_main_block', [], '', 'content', 'bartik', 0, '', '0');
     $this->assertEntity('bartik_search_form', 'search_form_block', [], '', 'sidebar_first', 'bartik', -1, '', '0');
     $this->assertEntity('bartik_user_login', 'user_login_block', [], '', 'sidebar_first', 'bartik', 0, '', '0');
     $this->assertEntity('bartik_system_powered-by', 'system_powered_by_block', [], '', 'footer', 'bartik', 10, '', '0');
     $this->assertEntity('seven_system_main', 'system_main_block', [], '', 'content', 'seven', 0, '', '0');
     $this->assertEntity('seven_user_login', 'user_login_block', [], '', 'content', 'seven', 10, '', '0');
     // The d7_custom_block migration should have migrated a block containing a
     // mildly amusing limerick. We'll need its UUID to determine
     // bartik_block_1's plugin ID.
     $uuid = BlockContent::load(1)->uuid();
     $this->assertEntity('bartik_block_1', 'block_content:' . $uuid, ['authenticated'], '', 'highlighted', 'bartik', 0, 'Mildly amusing limerick of the day', 'visible');
     // Assert that disabled blocks (or enabled blocks whose plugin IDs could
     // be resolved) did not migrate.
     $non_existent_blocks = ['bartik_system_navigation', 'bartik_system_help', 'seven_user_new', 'seven_search_form', 'bartik_comment_recent', 'bartik_node_syndicate', 'bartik_node_recent', 'bartik_shortcut_shortcuts', 'bartik_system_management', 'bartik_system_user-menu', 'bartik_system_main-menu', 'bartik_user_new', 'bartik_user_online', 'seven_comment_recent', 'seven_node_syndicate', 'seven_shortcut_shortcuts', 'seven_system_powered-by', 'seven_system_navigation', 'seven_system_management', 'seven_system_user-menu', 'seven_system_main-menu', 'seven_user_online', 'bartik_blog_recent', 'bartik_book_navigation', 'bartik_locale_language', 'bartik_forum_active', 'bartik_forum_new', 'seven_blog_recent', 'seven_book_navigation', 'seven_locale_language', 'seven_forum_active', 'seven_forum_new', 'bartik_menu_menu-test-menu', 'bartik_statistics_popular', 'seven_menu_menu-test-menu', 'seven_statistics_popular', 'seven_block_1'];
     $this->assertTrue(empty(Block::loadMultiple($non_existent_blocks)));
 }
 /**
  * Test deleting a block.
  */
 public function testBlockDelete()
 {
     // Create a block.
     $edit = array();
     $edit['info[0][value]'] = $this->randomMachineName(8);
     $body = $this->randomMachineName(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('block_content', 1);
     $url = 'admin/structure/block/add/block_content:' . $block->uuid() . '/' . \Drupal::config('system.theme')->get('default');
     $this->drupalPostForm($url, $instance, t('Save block'));
     $block = BlockContent::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->randomMachineName(8);
     $body2 = $this->randomMachineName(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->randomMachineName(8);
     $body = $this->randomMachineName(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');
 }