/** * 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"'); }
/** * 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; }
/** * 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 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 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; }
/** * Checks whether custom block IDs are saved properly during an import. */ public function testImport() { // Custom block ID must be a number that is not in the database. $max_id = db_query('SELECT MAX(id) FROM {block_content}')->fetchField(); $test_id = $max_id + mt_rand(1000, 1000000); $info = $this->randomMachineName(8); $block_array = array('info' => $info, 'body' => array('value' => $this->randomMachineName(32)), 'type' => 'basic', 'id' => $test_id); $block = BlockContent::create($block_array); $block->enforceIsNew(TRUE); $block->save(); // Verify that block_submit did not wipe the provided id. $this->assertEqual($block->id(), $test_id, 'Block imported using provide id'); // Test the import saved. $block_by_id = BlockContent::load($test_id); $this->assertTrue($block_by_id, 'Custom block load by block ID.'); $this->assertIdentical($block_by_id->body->value, $block_array['body']['value']); }
/** * 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); }
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>'); }