/** * Tests that Quick Edit's data- attributes are present for content blocks. */ public function testContentBlock() { \Drupal::service('module_installer')->install(array('block_content')); // Create and place a content_block block. $block = BlockContent::create(['info' => $this->randomMachineName(), 'type' => 'basic', 'langcode' => 'en']); $block->save(); $this->drupalPlaceBlock('block_content:' . $block->uuid()); // Check that the data- attribute is present. $this->drupalLogin($this->editorUser); $this->drupalGet(''); $this->assertRaw('data-quickedit-entity-id="block_content/1"'); }
/** * 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())); }
/** * 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.'); }
/** * Creates a custom block. * * @param bool|string $title * (optional) Title of block. When no value is given uses a random name. * Defaults to FALSE. * @param bool|string $bundle * (optional) Bundle name. When no value is given, defaults to * $this->bundle. Defaults to FALSE. * * @return \Drupal\block_content\Entity\BlockContent * Created custom block. */ protected function createBlockContent($title = FALSE, $bundle = FALSE) { $title = $title ?: $this->randomMachineName(); $bundle = $bundle ?: $this->bundle; $block_content = BlockContent::create(array('info' => $title, 'type' => $bundle, 'langcode' => 'en')); $block_content->save(); return $block_content; }
/** * Tests XSS coming from Block Content block info. */ protected function doBlockContentTest() { BlockContentType::create(['id' => 'basic', 'label' => 'basic', 'revision' => TRUE])->save(); BlockContent::create(['type' => 'basic', 'info' => '<script>alert("block_content");</script>'])->save(); $this->drupalGet(Url::fromRoute('block.admin_display')); $this->clickLink('<script>alert("block_content");</script>'); $this->assertRaw('<script>alert("block_content");</script>'); $this->assertNoRaw('<script>alert("block_content");</script>'); }
/** * Creates a custom block. * * @param bool|string $title * (optional) Title of block. When no value is given uses a random name. * Defaults to FALSE. * @param string $bundle * (optional) Bundle name. Defaults to 'basic'. * @param bool $save * (optional) Whether to save the block. Defaults to TRUE. * * @return \Drupal\block_content\Entity\BlockContent * Created custom block. */ protected function createBlockContent($title = FALSE, $bundle = 'basic', $save = TRUE) { $title = $title ?: $this->randomMachineName(); $block_content = BlockContent::create(array('info' => $title, 'type' => $bundle, 'langcode' => 'en')); if ($block_content && $save === TRUE) { $block_content->save(); } return $block_content; }
/** * 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); }
/** * Creates a custom block. * * @param array $settings * (optional) An associative array of settings for the block_content, as * used in entity_create(). * * @return \Drupal\block_content\Entity\BlockContent * Created custom block. */ protected function createBlockContent(array $settings = array()) { $status = 0; $settings += array('info' => $this->randomMachineName(), 'type' => 'basic', 'langcode' => 'en'); if ($block_content = BlockContent::create($settings)) { $status = $block_content->save(); } $this->assertEqual($status, SAVED_NEW, SafeMarkup::format('Created block content %info.', array('%info' => $block_content->label()))); return $block_content; }
/** * {@inheritdoc} */ protected function createEntity() { $block_content_type = BlockContentType::create(array('id' => 'basic', 'label' => 'basic', 'revision' => FALSE)); $block_content_type->save(); block_content_add_body_field($block_content_type->id()); // Create a "Llama" custom block. $block_content = BlockContent::create(array('info' => 'Llama', 'type' => 'basic', 'body' => array('value' => 'The name "llama" was adopted by European settlers from native Peruvians.', 'format' => 'plain_text'))); $block_content->save(); return $block_content; }
/** * 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 deleting a block_content updates the discovered block plugin. */ public function testDeletingBlockContentShouldClearPluginCache() { // Create a block content type. $block_content_type = BlockContentType::create(['id' => 'spiffy', 'label' => 'Mucho spiffy', 'description' => "Provides a block type that increases your site's spiffiness by upto 11%"]); $block_content_type->save(); // And a block content entity. $block_content = BlockContent::create(['info' => 'Spiffy prototype', 'type' => 'spiffy']); $block_content->save(); // Make sure the block content provides a derivative block plugin in the // block repository. /** @var \Drupal\Core\Block\BlockManagerInterface $block_manager */ $block_manager = $this->container->get('plugin.manager.block'); $plugin_id = 'block_content' . PluginBase::DERIVATIVE_SEPARATOR . $block_content->uuid(); $this->assertTrue($block_manager->hasDefinition($plugin_id)); // Now delete the block content entity. $block_content->delete(); // The plugin should no longer exist. $this->assertFalse($block_manager->hasDefinition($plugin_id)); }
/** * Create a block_content with revision and rest result count for both views. */ public function testBlockContentRevisionRelationship() { $block_content = BlockContent::create(array('info' => $this->randomMachineName(), 'type' => 'basic', 'langcode' => 'en')); $block_content->save(); // Create revision of the block_content. $block_content_revision = clone $block_content; $block_content_revision->setNewRevision(); $block_content_revision->save(); $column_map = array('revision_id' => 'revision_id', 'id_1' => 'id_1', 'block_content_field_data_block_content_field_revision_id' => 'block_content_field_data_block_content_field_revision_id'); // Here should be two rows. $view_id = Views::getView('test_block_content_revision_id'); $this->executeView($view_id, array($block_content->id())); $resultset_id = array(array('revision_id' => '1', 'id_1' => '1', 'block_content_field_data_block_content_field_revision_id' => '1'), array('revision_id' => '2', 'id_1' => '1', 'block_content_field_data_block_content_field_revision_id' => '1')); $this->assertIdenticalResultset($view_id, $resultset_id, $column_map); // There should be only one row with active revision 2. $view_revision_id = Views::getView('test_block_content_revision_revision_id'); $this->executeView($view_revision_id, array($block_content->id())); $resultset_revision_id = array(array('revision_id' => '2', 'id_1' => '1', 'block_content_field_data_block_content_field_revision_id' => '1')); $this->assertIdenticalResultset($view_revision_id, $resultset_revision_id, $column_map); }
/** * 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.'); }
/** * 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()))); }
public function testBlockTitleTokens() { $label = 'tokenblock'; $bundle = BlockContentType::create(array('id' => $label, 'label' => $label, 'revision' => FALSE)); $bundle->save(); $block_content = BlockContent::create(array('type' => $label, 'label' => '[current-page:title] block title', 'info' => 'Test token title block', 'body[value]' => 'This is the test token title block.')); $block_content->save(); $block = $this->drupalPlaceBlock('block_content:' . $block_content->uuid(), array('label' => '[user:name]')); $this->drupalGet($block->getSystemPath()); $this->drupalPostForm(NULL, array(), t('Save block')); // Ensure token validation is working on the block. $this->assertText('The Title is using the following invalid tokens: [user:name].'); // Create the block for real now with a valid title. $settings = $block->get('settings'); $settings['label'] = '[current-page:title] block title'; $block->set('settings', $settings); $block->save(); // Ensure that tokens are not double-escaped when output as a block title. $this->drupalCreateContentType(array('type' => 'page')); $node = $this->drupalCreateNode(array('title' => "Site's first node")); $this->drupalGet('node/' . $node->id()); // The apostraphe should only be escaped once. $this->assertRaw("Site's first node block title"); }
/** * Tests XSS coming from Block Content block info. */ protected function doBlockContentTest() { BlockContentType::create(['id' => 'basic', 'label' => 'basic', 'revision' => TRUE])->save(); BlockContent::create(['type' => 'basic', 'info' => '<script>alert("block_content");</script>'])->save(); $this->drupalGet(Url::fromRoute('block.admin_display')); $this->clickLinkPartialName('Place block'); $this->assertEscaped('<script>alert("block_content");</script>'); $this->assertNoRaw('<script>alert("block_content");</script>'); }
/** * 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'); }
/** * 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."); }
/** * 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))); }