/** * Logs in users, tests help pages. */ public function testHelp() { $this->drupalGet('help_page_test/has_help'); $this->assertText(t('I have help!')); $this->assertText($this->helpBlock->label()); $this->drupalGet('help_page_test/no_help'); // The help block should not appear when there is no help. $this->assertNoText($this->helpBlock->label()); }
/** * @param \Drupal\block\Entity\Block $block * * @return string */ protected function getGroupForBlock(Block $block) { /** @var ConditionPluginCollection $conditions */ $conditions = $block->getVisibilityConditions(); $config_block_visibility_group = ''; if ($conditions->has('condition_group')) { $condition_config = $conditions->get('condition_group') ->getConfiguration(); $config_block_visibility_group = $condition_config['block_visibility_group']; return $config_block_visibility_group; } return $config_block_visibility_group; }
/** * Logs in users, tests help pages. */ public function testHelp() { $this->drupalGet('help_page_test/has_help'); $this->assertText(t('I have help!')); $this->assertText($this->helpBlock->label()); $this->drupalGet('help_page_test/no_help'); // The help block should not appear when there is no help. $this->assertNoText($this->helpBlock->label()); // Ensure that if two hook_help() implementations both return a render array // the output is as expected. $this->drupalGet('help_page_test/test_array'); $this->assertText('Help text from more_help_page_test_help module.'); $this->assertText('Help text from help_page_test_help module.'); }
/** * Tests getDefaultConfigLangcode(). */ public function testGetDefaultConfigLangcode() { // Install the Language module's configuration so we can use the // module_installer service. $this->installConfig(['language']); $this->assertNull(\Drupal::service('locale.config_manager')->getDefaultConfigLangcode('locale_test_translate.settings'), 'Before installing a module the locale config manager can not access the shipped configuration.'); \Drupal::service('module_installer')->install(['locale_test_translate']); $this->assertEqual('en', \Drupal::service('locale.config_manager')->getDefaultConfigLangcode('locale_test_translate.settings'), 'After installing a module the locale config manager can get the shipped configuration langcode.'); $simple_config = \Drupal::configFactory()->getEditable('locale_test_translate.simple_config_extra'); $simple_config->set('foo', 'bar')->save(); $this->assertNull(\Drupal::service('locale.config_manager')->getDefaultConfigLangcode($simple_config->getName()), 'Simple config created through the API is not treated as shipped configuration.'); $block = Block::create(array('id' => 'test_default_config', 'theme' => 'classy', 'status' => TRUE, 'region' => 'content', 'plugin' => 'local_tasks_block', 'settings' => ['id' => 'local_tasks_block', 'label' => $this->randomMachineName(), 'provider' => 'core', 'label_display' => FALSE, 'primary' => TRUE, 'secondary' => TRUE])); $block->save(); // Install the theme after creating the block as installing the theme will // install the block provided by the locale_test module. \Drupal::service('theme_installer')->install(['classy']); // The test_default_config block provided by the locale_test module will not // be installed because a block with the same ID already exists. $this->installConfig(['locale_test']); $this->assertNull(\Drupal::service('locale.config_manager')->getDefaultConfigLangcode('block.block.test_default_config'), 'The block.block.test_default_config is not shipped configuration.'); // Delete the block so we can install the one provided by the locale_test // module. $block->delete(); $this->installConfig(['locale_test']); $this->assertEqual('en', \Drupal::service('locale.config_manager')->getDefaultConfigLangcode('block.block.test_default_config'), 'The block.block.test_default_config is shipped configuration.'); // Test the special case for configurable_language config entities. $fr_language = ConfigurableLanguage::createFromLangcode('fr'); $fr_language->save(); $this->assertEqual('en', \Drupal::service('locale.config_manager')->getDefaultConfigLangcode('language.entity.fr'), 'The language.entity.fr is treated as shipped configuration because it is a configurable_language config entity and in the standard language list.'); $custom_language = ConfigurableLanguage::createFromLangcode('custom'); $custom_language->save(); $this->assertNull(\Drupal::service('locale.config_manager')->getDefaultConfigLangcode('language.entity.custom'), 'The language.entity.custom is not shipped configuration because it is not in the standard language list.'); }
/** * Tests the rendering of blocks. */ public function testBasicRendering() { \Drupal::state()->set('block_test.content', ''); $entity = $this->controller->create(array('id' => 'test_block1', 'theme' => 'stark', 'plugin' => 'test_html')); $entity->save(); // Test the rendering of a block. $entity = Block::load('test_block1'); $output = entity_view($entity, 'block'); $expected = array(); $expected[] = '<div id="block-test-block1">'; $expected[] = ' '; $expected[] = ' '; $expected[] = ' '; $expected[] = ' </div>'; $expected[] = ''; $expected_output = implode("\n", $expected); $this->assertEqual($this->renderer->renderRoot($output), $expected_output); // Reset the HTML IDs so that the next render is not affected. Html::resetSeenIds(); // Test the rendering of a block with a given title. $entity = $this->controller->create(array('id' => 'test_block2', 'theme' => 'stark', 'plugin' => 'test_html', 'settings' => array('label' => 'Powered by Bananas'))); $entity->save(); $output = entity_view($entity, 'block'); $expected = array(); $expected[] = '<div id="block-test-block2">'; $expected[] = ' '; $expected[] = ' <h2>Powered by Bananas</h2>'; $expected[] = ' '; $expected[] = ' '; $expected[] = ' </div>'; $expected[] = ''; $expected_output = implode("\n", $expected); $this->assertEqual($this->renderer->renderRoot($output), $expected_output); }
public function index($count) { // Esta forma de llevar a cabo conexiones es mas rapida sin usar dependency injection // sin embargo es considerada mala practica // $connection = \Drupal::database(); $select = $this->db->select('node', 'n'); $select->fields('n'); $select->range(0, $count); $result = $select->execute()->fetchAll(); $query = $this->entityQuery->get('node'); $query->condition('status', 1); $query->range(0, $count); $result = $query->execute(); $storage = $this->entityManager->getStorage('node'); $render_controller = $this->entityManager->getViewBuilder('node'); $nodes = $storage->loadMultiple($result); $some_nodes = array(); foreach ($nodes as $nid => $node) { $some_nodes[$nid] = $node->getTitle(); } $dispatcher = $this->eventDisp; $e = new NodeFancyDisplayEvent($some_nodes); $event = $dispatcher->dispatch('rootstack_sandbox.fancy_node_edit', $e); $some_nodes = $event->getFancyNodes(); // Generar links en D8..... http://drupal.stackexchange.com/questions/144992/how-do-you-create-a-link-in-drupal-8 :( $block = \Drupal\block\Entity\Block::load('views_block__sdf_block_1'); $test = $this->entityManager->getViewBuilder('block')->view($block); return ['#theme' => 'fancy_nodes', '#summary' => $render_controller->view(array_values($nodes)[0]), '#some_nodes' => $some_nodes, '#total' => count($nodes)]; }
public function testUI() { // Set up a block and a entity_test entity. $block = Block::create(['id' => 'test_id', 'plugin' => 'system_main_block']); $block->save(); $entity_test = EntityTest::create(['bundle' => 'entity_test']); $entity_test->save(); $default = $this->randomView([]); $id = $default['id']; $view = View::load($id); $this->drupalGet($view->urlInfo('edit-form')); // Add a global NULL argument to the view for testing argument placeholders. $this->drupalPostForm("admin/structure/views/nojs/add-handler/{$id}/page_1/argument", ['name[views.null]' => 1], 'Add and configure contextual filters'); $this->drupalPostForm(NULL, [], 'Apply'); // Configure both the entity_test area header and the block header to // reference the given entities. $this->drupalPostForm("admin/structure/views/nojs/add-handler/{$id}/page_1/header", ['name[views.entity_block]' => 1], 'Add and configure header'); $this->drupalPostForm(NULL, ['options[target]' => $block->id()], 'Apply'); $this->drupalPostForm("admin/structure/views/nojs/add-handler/{$id}/page_1/header", ['name[views.entity_entity_test]' => 1], 'Add and configure header'); $this->drupalPostForm(NULL, ['options[target]' => $entity_test->id()], 'Apply'); $this->drupalPostForm(NULL, [], 'Save'); // Confirm the correct target identifiers were saved for both entities. $view = View::load($id); $header = $view->getDisplay('default')['display_options']['header']; $this->assertEqual(['entity_block', 'entity_entity_test'], array_keys($header)); $this->assertEqual($block->id(), $header['entity_block']['target']); $this->assertEqual($entity_test->uuid(), $header['entity_entity_test']['target']); // Confirm that the correct serial ID (for the entity_test) and config ID // (for the block) are displayed in the form. $this->drupalGet("admin/structure/views/nojs/handler/{$id}/page_1/header/entity_block"); $this->assertFieldByName('options[target]', $block->id()); $this->drupalGet("admin/structure/views/nojs/handler/{$id}/page_1/header/entity_entity_test"); $this->assertFieldByName('options[target]', $entity_test->id()); // Replace the header target entities with argument placeholders. $this->drupalPostForm("admin/structure/views/nojs/handler/{$id}/page_1/header/entity_block", ['options[target]' => '{{ raw_arguments.null }}'], 'Apply'); $this->drupalPostForm("admin/structure/views/nojs/handler/{$id}/page_1/header/entity_entity_test", ['options[target]' => '{{ raw_arguments.null }}'], 'Apply'); $this->drupalPostForm(NULL, [], 'Save'); // Confirm that the argument placeholders are saved. $view = View::load($id); $header = $view->getDisplay('default')['display_options']['header']; $this->assertEqual(['entity_block', 'entity_entity_test'], array_keys($header)); $this->assertEqual('{{ raw_arguments.null }}', $header['entity_block']['target']); $this->assertEqual('{{ raw_arguments.null }}', $header['entity_entity_test']['target']); // Confirm that the argument placeholders are still displayed in the form. $this->drupalGet("admin/structure/views/nojs/handler/{$id}/page_1/header/entity_block"); $this->assertFieldByName('options[target]', '{{ raw_arguments.null }}'); $this->drupalGet("admin/structure/views/nojs/handler/{$id}/page_1/header/entity_entity_test"); $this->assertFieldByName('options[target]', '{{ raw_arguments.null }}'); // Change the targets for both headers back to the entities. $this->drupalPostForm("admin/structure/views/nojs/handler/{$id}/page_1/header/entity_block", ['options[target]' => $block->id()], 'Apply'); $this->drupalPostForm("admin/structure/views/nojs/handler/{$id}/page_1/header/entity_entity_test", ['options[target]' => $entity_test->id()], 'Apply'); $this->drupalPostForm(NULL, [], 'Save'); // Confirm the targets were again saved correctly and not skipped based on // the previous form value. $view = View::load($id); $header = $view->getDisplay('default')['display_options']['header']; $this->assertEqual(['entity_block', 'entity_entity_test'], array_keys($header)); $this->assertEqual($block->id(), $header['entity_block']['target']); $this->assertEqual($entity_test->uuid(), $header['entity_entity_test']['target']); }
/** * {@inheritdoc} */ protected function setUpFixtures() { $this->installEntitySchema('user'); $this->installEntitySchema('entity_test'); $this->installConfig(['entity_test']); Block::create(['id' => 'test_block', 'plugin' => 'system_main_block'])->save(); parent::setUpFixtures(); }
/** * Tests the block config schema for block plugins. */ public function testBlockConfigSchema() { foreach ($this->blockManager->getDefinitions() as $block_id => $definition) { $id = strtolower($this->randomMachineName()); $block = Block::create(array('id' => $id, 'theme' => 'stark', 'weight' => 00, 'status' => TRUE, 'region' => 'content', 'plugin' => $block_id, 'settings' => array('label' => $this->randomMachineName(), 'provider' => 'system', 'label_display' => FALSE), 'visibility' => array())); $block->save(); $config = \Drupal::config("block.block.{$id}"); $this->assertEqual($config->get('id'), $id); $this->assertConfigSchema($this->typedConfig, $config->getName(), $config->get()); } }
/** * Tests the functionality of the Printable block. */ public function testPrintableBlock() { $this->drupalLogin($this->adminUser); $edit = ['id' => strtolower($this->randomMachineName()), 'settings[label]' => $this->randomMachineName(8), 'region' => 'sidebar_first', 'visibility[node_type][bundles][article]' => 'article']; $theme = \Drupal::service('theme_handler')->getDefault(); $this->drupalPostForm("admin/structure/block/add/printable_links_block%3Anode/{$theme}", $edit, t('Save block')); $block = Block::load($edit['id']); $visibility = $block->getVisibility(); $this->assertTrue(isset($visibility['node_type']['bundles']['article']), 'Visibility settings were saved to configuration'); // Test deleting the block from the edit form. $this->drupalGet('admin/structure/block/manage/' . $edit['id']); $this->clickLink(t('Delete')); $this->assertRaw(t('Are you sure you want to delete the block %name?', array('%name' => $edit['settings[label]']))); $this->drupalPostForm(NULL, array(), t('Delete')); $this->assertRaw(t('The block %name has been deleted.', array('%name' => $edit['settings[label]']))); }
/** * Tests that block context mapping is updated properly. */ public function testUpdateHookN() { $this->runUpdates(); $this->assertRaw('Encountered an unknown context mapping key coming probably from a contributed or custom module: One or more mappings could not be updated. Please manually review your visibility settings for the following blocks, which are disabled now:<ul><li>User login (Visibility: Baloney spam)</li></ul>'); // Disable maintenance mode. \Drupal::state()->set('system.maintenance_mode', FALSE); // We finished updating so we can login the user now. $this->drupalLogin($this->rootUser); // The block that we are testing has the following visibility rules: // - only visible on node pages // - only visible to authenticated users. $block_title = 'Test for 2354889'; // Create two nodes, a page and an article. $page = Node::create(['type' => 'page', 'title' => 'Page node']); $page->save(); $article = Node::create(['type' => 'article', 'title' => 'Article node']); $article->save(); // Check that the block appears only on Page nodes for authenticated users. $this->drupalGet('node/' . $page->id()); $this->assertRaw($block_title, 'Test block is visible on a Page node as an authenticated user.'); $this->drupalGet('node/' . $article->id()); $this->assertNoRaw($block_title, 'Test block is not visible on a Article node as an authenticated user.'); $this->drupalLogout(); // Check that the block does not appear on any page for anonymous users. $this->drupalGet('node/' . $page->id()); $this->assertNoRaw($block_title, 'Test block is not visible on a Page node as an anonymous user.'); $this->drupalGet('node/' . $article->id()); $this->assertNoRaw($block_title, 'Test block is not visible on a Article node as an anonymous user.'); // Ensure that all the context mappings got updated properly. $block = Block::load('testfor2354889'); $visibility = $block->get('visibility'); $this->assertEqual('@node.node_route_context:node', $visibility['node_type']['context_mapping']['node']); $this->assertEqual('@user.current_user_context:current_user', $visibility['user_role']['context_mapping']['user']); $this->assertEqual('@language.current_language_context:language_interface', $visibility['language']['context_mapping']['language']); // Check that a block with invalid context is being disabled and that it can // still be edited afterward. $disabled_block = Block::load('thirdtestfor2354889'); $this->assertFalse($disabled_block->status(), 'Block with invalid context is disabled'); $this->assertEqual(['thirdtestfor2354889' => ['missing_context_ids' => ['baloney.spam' => ['node_type']], 'status' => TRUE]], \Drupal::keyValue('update_backup')->get('block_update_8001')); $disabled_block_visibility = $disabled_block->get('visibility'); $this->assertTrue(!isset($disabled_block_visibility['node_type']), 'The problematic visibility condition has been removed.'); $admin_user = $this->drupalCreateUser(['administer blocks']); $this->drupalLogin($admin_user); $this->drupalGet('admin/structure/block/manage/thirdtestfor2354889'); $this->assertResponse('200'); }
/** * Tests if the visibility settings are removed if the language is deleted. */ public function testLanguageBlockVisibilityLanguageDelete() { // Enable a standard block and set the visibility setting for one language. $edit = array('visibility' => array('language' => array('language_type' => 'language_interface', 'langcodes' => array('fr' => 'fr')))); $block = $this->drupalPlaceBlock('system_powered_by_block', $edit); // Check that we have the language in config after saving the setting. $visibility = $block->getVisibility(); $language = $visibility['language']['langcodes']['fr']; $this->assertTrue('fr' === $language, 'Language is set in the block configuration.'); // Delete the language. $this->drupalPostForm('admin/config/regional/language/delete/fr', array(), t('Delete')); // Check that the language is no longer stored in the configuration after // it is deleted. $block = Block::load($block->id()); $visibility = $block->getVisibility(); $this->assertTrue(empty($visibility['language']['langcodes']['fr']), 'Language is no longer not set in the block configuration after deleting the block.'); }
/** * Tests hook invocations for CRUD operations on blocks. */ public function testBlockHooks() { $entity = entity_create('block', array('id' => 'stark_test_html', 'plugin' => 'test_html', 'theme' => 'stark')); $this->assertHookMessageOrder(array('entity_crud_hook_test_block_create called', 'entity_crud_hook_test_entity_create called for type block')); $GLOBALS['entity_crud_hook_test'] = array(); $entity->save(); $this->assertHookMessageOrder(array('entity_crud_hook_test_block_presave called', 'entity_crud_hook_test_entity_presave called for type block', 'entity_crud_hook_test_block_insert called', 'entity_crud_hook_test_entity_insert called for type block')); $GLOBALS['entity_crud_hook_test'] = array(); $entity = Block::load($entity->id()); $this->assertHookMessageOrder(array('entity_crud_hook_test_entity_load called for type block', 'entity_crud_hook_test_block_load called')); $GLOBALS['entity_crud_hook_test'] = array(); $entity->label = 'New label'; $entity->save(); $this->assertHookMessageOrder(array('entity_crud_hook_test_block_presave called', 'entity_crud_hook_test_entity_presave called for type block', 'entity_crud_hook_test_block_update called', 'entity_crud_hook_test_entity_update called for type block')); $GLOBALS['entity_crud_hook_test'] = array(); $entity->delete(); $this->assertHookMessageOrder(array('entity_crud_hook_test_block_predelete called', 'entity_crud_hook_test_entity_predelete called for type block', 'entity_crud_hook_test_block_delete called', 'entity_crud_hook_test_entity_delete called for type block')); }
/** * Tests template suggestions from block_theme_suggestions_block(). */ function testBlockThemeHookSuggestions() { // Define a block with a derivative to be preprocessed, which includes both // an underscore (not transformed) and a hyphen (transformed to underscore), // and generates possibilities for each level of derivative. // @todo Clarify this comment. $block = Block::create(array('plugin' => 'system_menu_block:admin', 'region' => 'footer', 'id' => 'machinename')); $variables = array(); $plugin = $block->getPlugin(); $variables['elements']['#configuration'] = $plugin->getConfiguration(); $variables['elements']['#plugin_id'] = $plugin->getPluginId(); $variables['elements']['#id'] = $block->id(); $variables['elements']['#base_plugin_id'] = $plugin->getBaseId(); $variables['elements']['#derivative_plugin_id'] = $plugin->getDerivativeId(); $variables['elements']['content'] = array(); $suggestions = block_theme_suggestions_block($variables); $this->assertEqual($suggestions, array('block__system', 'block__system_menu_block', 'block__system_menu_block__admin', 'block__machinename')); }
/** * Creates a block instance based on default settings. * * @param string $plugin_id * The plugin ID of the block type for this block instance. * @param array $settings * (optional) An associative array of settings for the block entity. * Override the defaults by specifying the key and value in the array, for * example: * @code * $this->drupalPlaceBlock('system_powered_by_block', array( * 'label' => t('Hello, world!'), * )); * @endcode * The following defaults are provided: * - label: Random string. * - ID: Random string. * - region: 'sidebar_first'. * - theme: The default theme. * - visibility: Empty array. * * @return \Drupal\block\Entity\Block * The block entity. * * @todo * Add support for creating custom block instances. */ protected function placeBlock($plugin_id, array $settings = array()) { $config = \Drupal::configFactory(); $settings += array('plugin' => $plugin_id, 'region' => 'sidebar_first', 'id' => strtolower($this->randomMachineName(8)), 'theme' => $config->get('system.theme')->get('default'), 'label' => $this->randomMachineName(8), 'visibility' => array(), 'weight' => 0); $values = []; foreach (array('region', 'id', 'theme', 'plugin', 'weight', 'visibility') as $key) { $values[$key] = $settings[$key]; // Remove extra values that do not belong in the settings array. unset($settings[$key]); } foreach ($values['visibility'] as $id => $visibility) { $values['visibility'][$id]['id'] = $id; } $values['settings'] = $settings; $block = Block::create($values); $block->save(); return $block; }
/** * Tests that local task blocks are configurable to show a specific level. */ public function testLocalTaskBlock() { // Remove the default block and create a new one. $this->sut->delete(); $this->sut = $this->drupalPlaceBlock('local_tasks_block', ['id' => 'tabs_block', 'primary' => TRUE, 'secondary' => FALSE]); $this->drupalGet(Url::fromRoute('menu_test.local_task_test_tasks_settings')); // Verify that local tasks in the first level appear. $this->assertLocalTasks([['menu_test.local_task_test_tasks_view', []], ['menu_test.local_task_test_tasks_edit', []], ['menu_test.local_task_test_tasks_settings', []]]); // Verify that local tasks in the second level doesn't appear. $this->assertNoLocalTasks(1); $this->sut->delete(); $this->sut = $this->drupalPlaceBlock('local_tasks_block', ['id' => 'tabs_block', 'primary' => FALSE, 'secondary' => TRUE]); $this->drupalGet(Url::fromRoute('menu_test.local_task_test_tasks_settings')); // Verify that local tasks in the first level doesn't appear. $this->assertNoLocalTasks(0); // Verify that local tasks in the second level appear. $sub_tasks = [['menu_test.local_task_test_tasks_settings_sub1', []], ['menu_test.local_task_test_tasks_settings_sub2', []], ['menu_test.local_task_test_tasks_settings_sub3', []], ['menu_test.local_task_test_tasks_settings_derived', ['placeholder' => 'derive1']], ['menu_test.local_task_test_tasks_settings_derived', ['placeholder' => 'derive2']]]; $this->assertLocalTasks($sub_tasks, 1); }
/** * {@inheritdoc} */ public function process($text, $langcode) { $new_text = $text; $filter_result = new FilterProcessResult($text); if (preg_match('/\\[survey\\:.+\\]/', $text, $result)) { $token = $result[0]; $start = strpos($token, ':') + 1; $length = strpos($token, ']') - $start; $id = substr($token, $start, $length); $block = Block::load($id); if ($block) { $replace = $block->get('settings')['html']; $new_text = str_replace($token, $replace, $text); $filter_result->setProcessedText($new_text); $filter_result->setCacheTags($block->getCacheTags()); } } return $filter_result; }
/** * Tests if the visibility settings are removed if the language is deleted. */ public function testLanguageBlockVisibilityLanguageDelete() { // Enable a standard block and set the visibility setting for one language. $edit = array('visibility' => array('language' => array('langcodes' => array('fr' => 'fr')))); $block = $this->drupalPlaceBlock('system_powered_by_block', $edit); // Check that we have the language in config after saving the setting. $visibility = $block->getVisibility(); $this->assertEqual('fr', $visibility['language']['langcodes']['fr'], 'Language is set in the block configuration.'); // Delete the language. $this->drupalPostForm('admin/config/regional/language/delete/fr', array(), t('Delete')); // Check that the language is no longer stored in the configuration after // it is deleted. $block = Block::load($block->id()); $visibility = $block->getVisibility(); $this->assertTrue(empty($visibility['language']['langcodes']['fr']), 'Language is no longer not set in the block configuration after deleting the block.'); // Ensure that the block visibility for language is gone from the UI. $this->drupalGet('admin/structure/block'); $this->clickLink('Configure'); $elements = $this->xpath('//details[@id="edit-visibility-language"]'); $this->assertTrue(empty($elements)); }
/** * Tests that blocks assigned to invalid regions work correctly. */ function testBlockInInvalidRegion() { // Enable a test block and place it in an invalid region. $block = $this->drupalPlaceBlock('test_html'); $block->setRegion('invalid_region'); $block->save(); $warning_message = t('The block %info was assigned to the invalid region %region and has been disabled.', array('%info' => $block->id(), '%region' => 'invalid_region')); // Clearing the cache should disable the test block placed in the invalid region. $this->drupalPostForm('admin/config/development/performance', array(), 'Clear all caches'); $this->assertRaw($warning_message, 'Enabled block was in the invalid region and has been disabled.'); // Clear the cache to check if the warning message is not triggered. $this->drupalPostForm('admin/config/development/performance', array(), 'Clear all caches'); $this->assertNoRaw($warning_message, 'Disabled block in the invalid region will not trigger the warning.'); // Place disabled test block in the invalid region of the default theme. $block = Block::load($block->id()); $block->setRegion('invalid_region'); $block->save(); // Clear the cache to check if the warning message is not triggered. $this->drupalPostForm('admin/config/development/performance', array(), 'Clear all caches'); $this->assertNoRaw($warning_message, 'Disabled block in the invalid region will not trigger the warning.'); }
/** * Tests block_user_role_delete. */ public function testBlockUserRoleDelete() { $role1 = Role::create(['id' => 'test_role1', 'name' => $this->randomString()]); $role1->save(); $role2 = Role::create(['id' => 'test_role2', 'name' => $this->randomString()]); $role2->save(); $block = Block::create(['id' => $this->randomMachineName(), 'plugin' => 'system_powered_by_block']); $block->setVisibilityConfig('user_role', ['roles' => [$role1->id() => $role1->id(), $role2->id() => $role2->id()]]); $block->save(); $this->assertEqual($block->getVisibility()['user_role']['roles'], [$role1->id() => $role1->id(), $role2->id() => $role2->id()]); $role1->delete(); $block = Block::load($block->id()); $this->assertEqual($block->getVisibility()['user_role']['roles'], [$role2->id() => $role2->id()]); }
/** * Tests menu block settings. */ protected function doTestMenuBlock() { $menu_id = $this->menu->id(); $block_id = $this->blockPlacements[$menu_id]; $this->drupalGet('admin/structure/block/manage/' . $block_id); $this->drupalPostForm(NULL, ['settings[depth]' => 3, 'settings[level]' => 2], t('Save block')); $block = Block::load($block_id); $settings = $block->getPlugin()->getConfiguration(); $this->assertEqual($settings['depth'], 3); $this->assertEqual($settings['level'], 2); // Reset settings. $block->getPlugin()->setConfigurationValue('depth', 0); $block->getPlugin()->setConfigurationValue('level', 1); $block->save(); }
/** * Tests the recent comments block. */ public function testRecentNodeBlock() { $this->drupalLogin($this->adminUser); // Disallow anonymous users to view content. user_role_change_permissions(RoleInterface::ANONYMOUS_ID, array('access content' => FALSE)); // Enable the recent content block with two items. $block = $this->drupalPlaceBlock('views_block:content_recent-block_1', array('id' => 'test_block', 'items_per_page' => 2)); // Test that block is not visible without nodes. $this->drupalGet(''); $this->assertText(t('No content available.'), 'Block with "No content available." found.'); // Add some test nodes. $default_settings = array('uid' => $this->webUser->id(), 'type' => 'article'); $node1 = $this->drupalCreateNode($default_settings); $node2 = $this->drupalCreateNode($default_settings); $node3 = $this->drupalCreateNode($default_settings); // Change the changed time for node so that we can test ordering. db_update('node_field_data')->fields(array('changed' => $node1->getChangedTime() + 100))->condition('nid', $node2->id())->execute(); db_update('node_field_data')->fields(array('changed' => $node1->getChangedTime() + 200))->condition('nid', $node3->id())->execute(); // Test that a user without the 'access content' permission cannot // see the block. $this->drupalLogout(); $this->drupalGet(''); $this->assertNoText($block->label(), 'Block was not found.'); // Test that only the 2 latest nodes are shown. $this->drupalLogin($this->webUser); $this->assertNoText($node1->label(), 'Node not found in block.'); $this->assertText($node2->label(), 'Node found in block.'); $this->assertText($node3->label(), 'Node found in block.'); // Check to make sure nodes are in the right order. $this->assertTrue($this->xpath('//div[@id="block-test-block"]//table/tbody/tr[position() = 1]/td/a[text() = "' . $node3->label() . '"]'), 'Nodes were ordered correctly in block.'); $this->drupalLogout(); $this->drupalLogin($this->adminUser); // Verify that the More link is shown and leads to the admin content page. $this->drupalGet(''); $this->clickLink('More'); $this->assertResponse('200'); $this->assertUrl('admin/content'); // Set the number of recent nodes to show to 10. $block->getPlugin()->setConfigurationValue('items_per_page', 10); $block->save(); // Post an additional node. $node4 = $this->drupalCreateNode($default_settings); // Test that all four nodes are shown. $this->drupalGet(''); $this->assertText($node1->label(), 'Node found in block.'); $this->assertText($node2->label(), 'Node found in block.'); $this->assertText($node3->label(), 'Node found in block.'); $this->assertText($node4->label(), 'Node found in block.'); // Enable the "Powered by Drupal" block only on article nodes. $edit = ['id' => strtolower($this->randomMachineName()), 'region' => 'sidebar_first', 'visibility[node_type][bundles][article]' => 'article']; $theme = \Drupal::service('theme_handler')->getDefault(); $this->drupalPostForm("admin/structure/block/add/system_powered_by_block/{$theme}", $edit, t('Save block')); $block = Block::load($edit['id']); $visibility = $block->getVisibility(); $this->assertTrue(isset($visibility['node_type']['bundles']['article']), 'Visibility settings were saved to configuration'); // Create a page node. $node5 = $this->drupalCreateNode(array('uid' => $this->adminUser->id(), 'type' => 'page')); $this->drupalLogout(); $this->drupalLogin($this->webUser); // Verify visibility rules. $this->drupalGet(''); $label = $block->label(); $this->assertNoText($label, 'Block was not displayed on the front page.'); $this->drupalGet('node/add/article'); $this->assertText($label, 'Block was displayed on the node/add/article page.'); $this->drupalGet('node/' . $node1->id()); $this->assertText($label, 'Block was displayed on the node/N when node is of type article.'); $this->drupalGet('node/' . $node5->id()); $this->assertNoText($label, 'Block was not displayed on nodes of type page.'); $this->drupalLogin($this->adminUser); $this->drupalGet('admin/structure/block'); $this->assertText($label, 'Block was displayed on the admin/structure/block page.'); $this->assertLinkByHref($block->url()); }
/** * 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 the block settings migration. */ public function testBlockMigration() { $blocks = Block::loadMultiple(); $this->assertEqual(count($blocks), 8); // User blocks $test_block_user = $blocks['user']; $this->assertNotNull($test_block_user); $this->assertEqual('sidebar_first', $test_block_user->get('region')); $this->assertEqual('bartik', $test_block_user->get('theme')); $visibility = $test_block_user->getVisibility(); $this->assertEqual(TRUE, $visibility['request_path']['negate']); $this->assertEqual('', $visibility['request_path']['pages']); $this->assertEqual(0, $test_block_user->weight); $test_block_user_1 = $blocks['user_1']; $this->assertNotNull($test_block_user_1); $this->assertEqual('sidebar_first', $test_block_user_1->get('region')); $this->assertEqual('bartik', $test_block_user_1->get('theme')); $visibility = $test_block_user_1->getVisibility(); $this->assertEqual(TRUE, $visibility['request_path']['negate']); $this->assertEqual('', $visibility['request_path']['pages']); $this->assertEqual(0, $test_block_user_1->weight); // Check system block $test_block_system = $blocks['system']; $this->assertNotNull($test_block_system); $this->assertEqual('footer', $test_block_system->get('region')); $this->assertEqual('bartik', $test_block_system->get('theme')); $visibility = $test_block_system->getVisibility(); $this->assertEqual(TRUE, $visibility['request_path']['negate']); $this->assertEqual('', $visibility['request_path']['pages']); $this->assertEqual(-5, $test_block_system->weight); // Check menu blocks $test_block_menu = $blocks['menu']; $this->assertNotNull($test_block_menu); $this->assertEqual('header', $test_block_menu->get('region')); $this->assertEqual('bartik', $test_block_menu->get('theme')); $visibility = $test_block_menu->getVisibility(); $this->assertEqual(TRUE, $visibility['request_path']['negate']); $this->assertEqual('', $visibility['request_path']['pages']); $this->assertEqual(-5, $test_block_menu->weight); // Check custom blocks $test_block_block = $blocks['block']; $this->assertNotNull($test_block_block); $this->assertEqual('content', $test_block_block->get('region')); $this->assertEqual('bartik', $test_block_block->get('theme')); $visibility = $test_block_block->getVisibility(); $this->assertEqual(FALSE, $visibility['request_path']['negate']); $this->assertEqual('<front>', $visibility['request_path']['pages']); $this->assertEqual(0, $test_block_block->weight); $test_block_block_1 = $blocks['block_1']; $this->assertNotNull($test_block_block_1); $this->assertEqual('right', $test_block_block_1->get('region')); $this->assertEqual('bluemarine', $test_block_block_1->get('theme')); $visibility = $test_block_block_1->getVisibility(); $this->assertEqual(FALSE, $visibility['request_path']['negate']); $this->assertEqual('node', $visibility['request_path']['pages']); $this->assertEqual(-4, $test_block_block_1->weight); $test_block_block_2 = $blocks['block_2']; $this->assertNotNull($test_block_block_2); $this->assertEqual('right', $test_block_block_2->get('region')); $this->assertEqual('test_theme', $test_block_block_2->get('theme')); $visibility = $test_block_block_2->getVisibility(); $this->assertEqual(TRUE, $visibility['request_path']['negate']); $this->assertEqual('', $visibility['request_path']['pages']); $this->assertEqual(-7, $test_block_block_2->weight); $test_block_block_3 = $blocks['block_3']; $this->assertNotNull($test_block_block_3); $this->assertEqual('left', $test_block_block_3->get('region')); $this->assertEqual('test_theme', $test_block_block_3->get('theme')); $visibility = $test_block_block_3->getVisibility(); $this->assertEqual(TRUE, $visibility['request_path']['negate']); $this->assertEqual('', $visibility['request_path']['pages']); $this->assertEqual(-2, $test_block_block_3->weight); }
/** * Test the block settings migration. */ public function testBlockMigration() { $blocks = Block::loadMultiple(); $this->assertIdentical(count($blocks), 10); // User blocks $test_block_user = $blocks['user']; $this->assertNotNull($test_block_user); $this->assertIdentical('sidebar_first', $test_block_user->getRegion()); $this->assertIdentical('bartik', $test_block_user->getTheme()); $visibility = $test_block_user->getVisibility(); $this->assertTrue(empty($visibility)); $this->assertIdentical(0, $test_block_user->getWeight()); $test_block_user_1 = $blocks['user_1']; $this->assertNotNull($test_block_user_1); $this->assertIdentical('sidebar_first', $test_block_user_1->getRegion()); $this->assertIdentical('bartik', $test_block_user_1->getTheme()); $visibility = $test_block_user_1->getVisibility(); $this->assertTrue(empty($visibility)); $this->assertIdentical(0, $test_block_user_1->getWeight()); $test_block_user_2 = $blocks['user_2']; $this->assertNotNull($test_block_user_2); $this->assertIdentical('sidebar_second', $test_block_user_2->getRegion()); $this->assertIdentical('bartik', $test_block_user_2->getTheme()); $visibility = $test_block_user_2->getVisibility(); $this->assertIdentical($visibility['user_role']['id'], 'user_role'); $roles = array(); $roles['authenticated'] = 'authenticated'; $this->assertIdentical($visibility['user_role']['roles'], $roles); $this->assertFalse($visibility['user_role']['negate']); $this->assertIdentical(-9, $test_block_user_2->getWeight()); $test_block_user_3 = $blocks['user_3']; $this->assertNotNull($test_block_user_3); $this->assertIdentical('sidebar_second', $test_block_user_3->getRegion()); $this->assertIdentical('bartik', $test_block_user_3->getTheme()); $visibility = $test_block_user_3->getVisibility(); $this->assertIdentical($visibility['user_role']['id'], 'user_role'); $roles = array(); $roles['migrate_test_role_1'] = 'migrate_test_role_1'; $this->assertIdentical($visibility['user_role']['roles'], $roles); $this->assertFalse($visibility['user_role']['negate']); $this->assertIdentical(-6, $test_block_user_3->getWeight()); // Check system block $test_block_system = $blocks['system']; $this->assertNotNull($test_block_system); $this->assertIdentical('footer', $test_block_system->getRegion()); $this->assertIdentical('bartik', $test_block_system->getTheme()); $visibility = $test_block_system->getVisibility(); $this->assertIdentical('request_path', $visibility['request_path']['id']); $this->assertIdentical('node/1', $visibility['request_path']['pages']); $this->assertTrue($visibility['request_path']['negate']); $this->assertIdentical(-5, $test_block_system->getWeight()); // Check menu blocks $test_block_menu = $blocks['menu']; $this->assertNotNull($test_block_menu); $this->assertIdentical('header', $test_block_menu->getRegion()); $this->assertIdentical('bartik', $test_block_menu->getTheme()); $visibility = $test_block_menu->getVisibility(); $this->assertTrue(empty($visibility)); $this->assertIdentical(-5, $test_block_menu->getWeight()); // Check custom blocks $test_block_block = $blocks['block']; $this->assertNotNull($test_block_block); $this->assertIdentical('content', $test_block_block->getRegion()); $this->assertIdentical('bartik', $test_block_block->getTheme()); $visibility = $test_block_block->getVisibility(); $this->assertIdentical('request_path', $visibility['request_path']['id']); $this->assertIdentical('<front>', $visibility['request_path']['pages']); $this->assertFalse($visibility['request_path']['negate']); $this->assertIdentical(0, $test_block_block->getWeight()); $test_block_block_1 = $blocks['block_1']; $this->assertNotNull($test_block_block_1); $this->assertIdentical('right', $test_block_block_1->getRegion()); $this->assertIdentical('bluemarine', $test_block_block_1->getTheme()); $visibility = $test_block_block_1->getVisibility(); $this->assertIdentical('request_path', $visibility['request_path']['id']); $this->assertIdentical('node', $visibility['request_path']['pages']); $this->assertFalse($visibility['request_path']['negate']); $this->assertIdentical(-4, $test_block_block_1->getWeight()); $test_block_block_2 = $blocks['block_2']; $this->assertNotNull($test_block_block_2); $this->assertIdentical('right', $test_block_block_2->getRegion()); $this->assertIdentical('test_theme', $test_block_block_2->getTheme()); $visibility = $test_block_block_2->getVisibility(); $this->assertTrue(empty($visibility)); $this->assertIdentical(-7, $test_block_block_2->getWeight()); $test_block_block_3 = $blocks['block_3']; $this->assertNotNull($test_block_block_3); $this->assertIdentical('left', $test_block_block_3->getRegion()); $this->assertIdentical('test_theme', $test_block_block_3->getTheme()); $visibility = $test_block_block_3->getVisibility(); $this->assertTrue(empty($visibility)); $this->assertIdentical(-2, $test_block_block_3->getWeight()); }
/** * Tests if the language switcher block gets deleted when a language type has been made not configurable. */ public function testDisableLanguageSwitcher() { $block_id = 'test_language_block'; // Enable the language switcher block. $this->drupalPlaceBlock('language_block:' . LanguageInterface::TYPE_CONTENT, array('id' => $block_id)); // Check if the language switcher block has been created. $block = Block::load($block_id); $this->assertTrue($block, 'Language switcher block was created.'); // Make sure language_content is not configurable. $edit = array('language_content[configurable]' => FALSE); $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings')); $this->assertResponse(200); // Check if the language switcher block has been removed. $block = Block::load($block_id); $this->assertFalse($block, 'Language switcher block was removed.'); }
/** * Tests the block migration. */ public function testBlockMigration() { $blocks = Block::loadMultiple(); $this->assertIdentical(9, count($blocks)); // User blocks $visibility = []; $visibility['request_path']['id'] = 'request_path'; $visibility['request_path']['negate'] = TRUE; $visibility['request_path']['pages'] = "<front>\n/node/1\n/blog/*"; $this->assertEntity('user', $visibility, 'sidebar_first', 'bartik', 0); $visibility = []; $this->assertEntity('user_1', $visibility, 'sidebar_first', 'bartik', 0); $visibility['user_role']['id'] = 'user_role'; $roles['authenticated'] = 'authenticated'; $visibility['user_role']['roles'] = $roles; $context_mapping['user'] = '******'; $visibility['user_role']['context_mapping'] = $context_mapping; $visibility['user_role']['negate'] = FALSE; $this->assertEntity('user_2', $visibility, 'sidebar_second', 'bartik', -9); $visibility = []; $visibility['user_role']['id'] = 'user_role'; $visibility['user_role']['roles'] = ['migrate_test_role_1' => 'migrate_test_role_1']; $context_mapping['user'] = '******'; $visibility['user_role']['context_mapping'] = $context_mapping; $visibility['user_role']['negate'] = FALSE; $this->assertEntity('user_3', $visibility, 'sidebar_second', 'bartik', -6); // Check system block $visibility = []; $visibility['request_path']['id'] = 'request_path'; $visibility['request_path']['negate'] = TRUE; $visibility['request_path']['pages'] = '/node/1'; $this->assertEntity('system', $visibility, 'footer', 'bartik', -5); // Check menu blocks $visibility = []; $this->assertEntity('menu', $visibility, 'header', 'bartik', -5); // Check custom blocks $visibility['request_path']['id'] = 'request_path'; $visibility['request_path']['negate'] = FALSE; $visibility['request_path']['pages'] = '<front>'; $this->assertEntity('block', $visibility, 'content', 'bartik', 0); $visibility['request_path']['id'] = 'request_path'; $visibility['request_path']['negate'] = FALSE; $visibility['request_path']['pages'] = '/node'; $this->assertEntity('block_1', $visibility, 'sidebar_second', 'bluemarine', -4); $visibility = []; $this->assertEntity('block_2', $visibility, 'right', 'test_theme', -7); // Custom block with php code is not migrated. $block = Block::load('block_3'); $this->assertFalse($block instanceof Block); }
/** * Find a block instance on the page. * * @param \Drupal\block\Entity\Block $block * The block entity to find on the page. * * @return array * The result from the xpath query. */ protected function findBlockInstance(Block $block) { return $this->xpath('//div[@id = :id]', array(':id' => 'block-' . $block->id())); }
/** * Control access to a block instance. * * Modules may implement this hook if they want to have a say in whether or not * a given user has access to perform a given operation on a block instance. * * @param \Drupal\block\Entity\Block $block * The block instance. * @param string $operation * The operation to be performed; for instance, 'view', 'create', 'delete', or * 'update'. * @param \Drupal\Core\Session\AccountInterface $account * The user object to perform the access check operation on. * * @return \Drupal\Core\Access\AccessResultInterface * The access result. If all implementations of this hook return * AccessResultInterface objects whose value is !isAllowed() and * !isForbidden(), then default access rules from * \Drupal\block\BlockAccessControlHandler::checkAccess() are used. * * @see \Drupal\Core\Entity\EntityAccessControlHandler::access() * @see \Drupal\block\BlockAccessControlHandler::checkAccess() * @ingroup block_api */ function hook_block_access(\Drupal\block\Entity\Block $block, $operation, \Drupal\Core\Session\AccountInterface $account) { // Example code that would prevent displaying the 'Powered by Drupal' block in // a region different than the footer. if ($operation == 'view' && $block->getPluginId() == 'system_powered_by_block') { return AccessResult::forbiddenIf($block->getRegion() != 'footer')->cacheUntilEntityChanges($block); } // No opinion. return AccessResult::neutral(); }
/** * Tests the block migration. */ public function testBlockMigration() { $blocks = Block::loadMultiple(); $this->assertIdentical(9, count($blocks)); // User blocks $visibility = []; $visibility['request_path']['id'] = 'request_path'; $visibility['request_path']['negate'] = TRUE; $visibility['request_path']['pages'] = "<front>\n/node/1\n/blog/*"; $this->assertEntity('user', $visibility, 'sidebar_first', 'bartik', 0, '', '0'); $visibility = []; $this->assertEntity('user_1', $visibility, 'sidebar_first', 'bartik', 0, '', '0'); $visibility['user_role']['id'] = 'user_role'; $roles['authenticated'] = 'authenticated'; $visibility['user_role']['roles'] = $roles; $context_mapping['user'] = '******'; $visibility['user_role']['context_mapping'] = $context_mapping; $visibility['user_role']['negate'] = FALSE; $this->assertEntity('user_2', $visibility, 'sidebar_second', 'bartik', -9, '', '0'); $visibility = []; $visibility['user_role']['id'] = 'user_role'; $visibility['user_role']['roles'] = ['migrate_test_role_1' => 'migrate_test_role_1']; $context_mapping['user'] = '******'; $visibility['user_role']['context_mapping'] = $context_mapping; $visibility['user_role']['negate'] = FALSE; $this->assertEntity('user_3', $visibility, 'sidebar_second', 'bartik', -6, '', '0'); // Check system block $visibility = []; $visibility['request_path']['id'] = 'request_path'; $visibility['request_path']['negate'] = TRUE; $visibility['request_path']['pages'] = '/node/1'; $this->assertEntity('system', $visibility, 'footer_fifth', 'bartik', -5, '', '0'); // Check menu blocks $visibility = []; $this->assertEntity('menu', $visibility, 'header', 'bartik', -5, '', '0'); // Check custom blocks $visibility['request_path']['id'] = 'request_path'; $visibility['request_path']['negate'] = FALSE; $visibility['request_path']['pages'] = '<front>'; $this->assertEntity('block', $visibility, 'content', 'bartik', 0, 'Static Block', 'visible'); $visibility['request_path']['id'] = 'request_path'; $visibility['request_path']['negate'] = FALSE; $visibility['request_path']['pages'] = '/node'; // bluemarine does not exist in Drupal 8 and the d6_block migration defines // no mapping for its regions, so this block should have been defaulted // to the 'content' region. $this->assertEntity('block_1', $visibility, 'content', 'bluemarine', -4, 'Another Static Block', 'visible'); $visibility = []; $this->assertEntity('block_2', $visibility, 'right', 'test_theme', -7, '', '0'); // Custom block with php code is not migrated. $block = Block::load('block_3'); $this->assertFalse($block instanceof Block); }