/** * Tests the filter UI. */ public function testFilterUI() { $this->drupalGet('admin/structure/views/nojs/handler/test_filter_taxonomy_index_tid/default/filter/tid'); $result = $this->xpath('//select[@id="edit-options-value"]/option'); // Ensure that the expected hierarchy is available in the UI. $counter = 0; for ($i = 0; $i < 3; $i++) { for ($j = 0; $j <= $i; $j++) { $option = $result[$counter++]; $prefix = $this->terms[$i][$j]->parent->target_id ? '-' : ''; $attributes = $option->attributes(); $tid = (string) $attributes->value; $this->assertEqual($prefix . $this->terms[$i][$j]->getName(), (string) $option); $this->assertEqual($this->terms[$i][$j]->id(), $tid); } } // Ensure the autocomplete input element appears when using the 'textfield' // type. $view = View::load('test_filter_taxonomy_index_tid'); $display =& $view->getDisplay('default'); $display['display_options']['filters']['tid']['type'] = 'textfield'; $view->save(); $this->drupalGet('admin/structure/views/nojs/handler/test_filter_taxonomy_index_tid/default/filter/tid'); $this->assertFieldByXPath('//input[@id="edit-options-value"]'); // Tests \Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTid::calculateDependencies(). $expected = ['config' => ['taxonomy.vocabulary.tags'], 'content' => ['taxonomy_term:tags:' . Term::load(2)->uuid()], 'module' => ['node', 'taxonomy', 'user']]; $this->assertIdentical($expected, $view->calculateDependencies()->getDependencies()); }
/** * Tests the views.executable container service. */ public function testFactoryService() { $factory = $this->container->get('views.executable'); $this->assertTrue($factory instanceof ViewExecutableFactory, 'A ViewExecutableFactory instance was returned from the container.'); $view = View::load('test_executable_displays'); $this->assertTrue($factory->get($view) instanceof ViewExecutable, 'A ViewExecutable instance was returned from the factory.'); }
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']); }
/** * Tests that boolean filter values are updated properly. */ public function testViewsPostUpdateBooleanFilterValues() { $this->runUpdates(); // Load and initialize our test view. $view = View::load('test_boolean_filter_values'); $data = $view->toArray(); // Check that the field is using the expected string value. $this->assertIdentical('1', $data['display']['default']['display_options']['filters']['status']['value']); }
/** * Tests that field handlers are updated properly. */ public function testViewsUpdate8004() { $this->runUpdates(); // Load and initialize our test view. $view = View::load('test_duplicate_field_handlers'); $data = $view->toArray(); // Check that the field is using the expected base table. $this->assertEqual('node_field_data', $data['display']['default']['display_options']['fields']['nid']['table']); $this->assertEqual('node_field_data', $data['display']['default']['display_options']['filters']['type']['table']); $this->assertEqual('node_field_data', $data['display']['default']['display_options']['sorts']['vid']['table']); $this->assertEqual('node_field_data', $data['display']['default']['display_options']['arguments']['nid']['table']); }
/** * Tests the title area handler. */ public function testTitleText() { $this->drupalGet('test-area-title'); $this->assertTitle('test_title_header | Drupal'); // Check the view to return no result. /** @var \Drupal\views\Entity\View $view */ $view = View::load('test_area_title'); $display =& $view->getDisplay('default'); $display['display_options']['filters']['id'] = ['field' => 'id', 'id' => 'id', 'table' => 'views_test_data', 'relationship' => 'none', 'plugin_id' => 'numeric', 'provider' => 'views_test_data', 'value' => ['value' => '042118160112']]; $view->save(); $this->drupalGet('test-area-title'); $this->assertTitle('test_title_empty | Drupal'); }
/** * Creates a feed and checks that feed's page. */ public function testFeedPage() { // Increase the number of items published in the rss.xml feed so we have // enough articles to test paging. $view = View::load('frontpage'); $display =& $view->getDisplay('feed_1'); $display['display_options']['pager']['options']['items_per_page'] = 30; $view->save(); // Create a feed with 30 items. $this->createSampleNodes(30); $feed = $this->createFeed(); $this->updateFeedItems($feed, 30); // Check for presence of an aggregator pager. $this->drupalGet('aggregator'); $elements = $this->xpath("//ul[contains(@class, :class)]", array(':class' => 'pager__items')); $this->assertTrue(!empty($elements), 'Individual source page contains a pager.'); // Check for sources page title. $this->drupalGet('aggregator/sources'); $titles = $this->xpath('//h1[normalize-space(text())=:title]', array(':title' => 'Sources')); $this->assertTrue(!empty($titles), 'Source page contains correct title.'); // Find the expected read_more link on the sources page. $href = $feed->url(); $links = $this->xpath('//a[@href = :href]', array(':href' => $href)); $this->assertTrue(isset($links[0]), SafeMarkup::format('Link to href %href found.', array('%href' => $href))); $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags'); $cache_tags = explode(' ', $cache_tags_header); $this->assertTrue(in_array('aggregator_feed:' . $feed->id(), $cache_tags)); // Check the rss aggregator page as anonymous user. $this->drupalLogout(); $this->drupalGet('aggregator/rss'); $this->assertResponse(403); // Check the rss aggregator page as admin. $this->drupalLogin($this->adminUser); $this->drupalGet('aggregator/rss'); $this->assertResponse(200); $this->assertEqual($this->drupalGetHeader('Content-type'), 'application/rss+xml; charset=utf-8'); // Check the opml aggregator page. $this->drupalGet('aggregator/opml'); $outline = $this->xpath('//outline[1]'); $this->assertEqual($outline[0]['type'], 'rss', 'The correct type attribute is used for rss OPML.'); $this->assertEqual($outline[0]['text'], $feed->label(), 'The correct text attribute is used for rss OPML.'); $this->assertEqual($outline[0]['xmlurl'], $feed->getUrl(), 'The correct xmlUrl attribute is used for rss OPML.'); // Check for the presence of a pager. $this->drupalGet('aggregator/sources/' . $feed->id()); $elements = $this->xpath("//ul[contains(@class, :class)]", array(':class' => 'pager__items')); $this->assertTrue(!empty($elements), 'Individual source page contains a pager.'); $cache_tags = explode(' ', $this->drupalGetHeader('X-Drupal-Cache-Tags')); $this->assertTrue(in_array('aggregator_feed:' . $feed->id(), $cache_tags)); $this->assertTrue(in_array('aggregator_feed_view', $cache_tags)); $this->assertTrue(in_array('aggregator_item_view', $cache_tags)); }
/** * Tests post update function fixes dependencies. * * @see views_post_update_taxonomy_index_tid() */ public function testPostUpdateFunction() { /** @var \Drupal\views\Entity\View $view */ $view = View::load('test_filter_taxonomy_index_tid__non_existing_dependency'); // Dependencies are sorted. $content_dependencies = [$this->terms[3]->getConfigDependencyName(), $this->terms[4]->getConfigDependencyName()]; sort($content_dependencies); $this->assertEqual(['config' => ['taxonomy.vocabulary.tags'], 'content' => $content_dependencies, 'module' => ['node', 'taxonomy', 'user']], $view->calculateDependencies()->getDependencies()); $this->terms[3]->delete(); \Drupal::moduleHandler()->loadInclude('views', 'post_update.php'); views_post_update_taxonomy_index_tid(); $view = View::load('test_filter_taxonomy_index_tid__non_existing_dependency'); $this->assertEqual(['config' => ['taxonomy.vocabulary.tags'], 'content' => [$this->terms[4]->getConfigDependencyName()], 'module' => ['node', 'taxonomy', 'user']], $view->getDependencies()); }
/** * Tests query plugins settings. */ public function testQueryUI() { $view = View::load('test_view'); $display =& $view->getDisplay('default'); $display['display_options']['query'] = ['type' => 'query_test']; $view->save(); // Save some query settings. $query_settings_path = "admin/structure/views/nojs/display/test_view/default/query"; $random_value = $this->randomMachineName(); $this->drupalPostForm($query_settings_path, array('query[options][test_setting]' => $random_value), t('Apply')); $this->drupalPostForm(NULL, array(), t('Save')); // Check that the settings are saved into the view itself. $view = Views::getView('test_view'); $view->initDisplay(); $view->initQuery(); $this->assertEqual($random_value, $view->query->options['test_setting'], 'Query settings got saved'); }
/** * Tests the updating of views dependencies to image styles. */ public function testUpdateImageStyleDependencies() { $config_dependencies = View::load('foo')->getDependencies()['config']; // Checks that 'thumbnail' image style is not a dependency of view 'foo'. $this->assertFalse(in_array('image.style.thumbnail', $config_dependencies)); // We test the case the the field formatter image style doesn't exist. // Checks that 'nonexistent' image style is not a dependency of view 'foo'. $this->assertFalse(in_array('image.style.nonexistent', $config_dependencies)); // Run updates. $this->runUpdates(); $config_dependencies = View::load('foo')->getDependencies()['config']; // Checks that 'thumbnail' image style is a dependency of view 'foo'. $this->assertTrue(in_array('image.style.thumbnail', $config_dependencies)); // The 'nonexistent' style doesn't exist, thus is not a dependency. Checks // that 'nonexistent' image style is a not dependency of view 'foo'. $this->assertFalse(in_array('image.style.nonexistent', $config_dependencies)); }
/** * Tests that role filter dependencies are calculated correctly. */ public function testDependencies() { $role = Role::create(['id' => 'test_user_role']); $role->save(); $view = View::load('test_user_name'); $expected = ['module' => ['user']]; $this->assertEqual($expected, $view->getDependencies()); $display =& $view->getDisplay('default'); $display['display_options']['filters']['roles_target_id'] = ['id' => 'roles_target_id', 'table' => 'user__roles', 'field' => 'roles_target_id', 'value' => ['test_user_role' => 'test_user_role'], 'plugin_id' => 'user_roles']; $view->save(); $expected['config'][] = 'user.role.test_user_role'; $this->assertEqual($expected, $view->getDependencies()); $view = View::load('test_user_name'); $display =& $view->getDisplay('default'); $display['display_options']['filters']['roles_target_id'] = ['id' => 'roles_target_id', 'table' => 'user__roles', 'field' => 'roles_target_id', 'value' => [], 'plugin_id' => 'user_roles']; $view->save(); unset($expected['config']); $this->assertEqual($expected, $view->getDependencies()); }
/** * @covers ::calculateDependencies */ public function testCalculateDepenencies() { /** @var \Drupal\views\Entity\View $view */ $view = View::load('test_serializer_display_entity'); $display =& $view->getDisplay('rest_export_1'); $display['display_options']['defaults']['style'] = FALSE; $display['display_options']['style']['type'] = 'serializer'; $display['display_options']['style']['options']['formats'] = ['json', 'xml']; $view->save(); $view->calculateDependencies(); $this->assertEquals(['module' => ['rest', 'serialization', 'user']], $view->getDependencies()); \Drupal::service('module_installer')->install(['hal']); $view = View::load('test_serializer_display_entity'); $display =& $view->getDisplay('rest_export_1'); $display['display_options']['style']['options']['formats'] = ['json', 'xml', 'hal_json']; $view->save(); $view->calculateDependencies(); $this->assertEquals(['module' => ['hal', 'rest', 'serialization', 'user']], $view->getDependencies()); }
/** * Ensures that %1 and !1 are converted to twig tokens in existing views. */ public function testArgumentPlaceholderUpdate() { $this->runUpdates(); $view = View::load('test_token_view'); $data = $view->toArray(); $this->assertEqual('{{ arguments.nid }}-test-class-{{ raw_arguments.nid }}', $data['display']['default']['display_options']['style']['options']['col_class_custom']); $this->assertEqual('{{ arguments.nid }}-test-class-{{ raw_arguments.nid }}', $data['display']['default']['display_options']['style']['options']['row_class_custom']); $this->assertEqual('{{ arguments.nid }}-description-{{ raw_arguments.nid }}', $data['display']['feed_1']['display_options']['style']['options']['description']); $this->assertEqual('{{ arguments.nid }}-custom-text-{{ raw_arguments.nid }}', $data['display']['default']['display_options']['fields']['title']['alter']['text']); $this->assertEqual('test_token_view {{ arguments.nid }} {{ raw_arguments.nid }}', $data['display']['default']['display_options']['title']); $this->assertEqual('{{ arguments.nid }}-custom-{{ raw_arguments.nid }}', $data['display']['default']['display_options']['header']['area_text_custom']['content']); $this->assertEqual('{{ arguments.nid }}-text-{{ raw_arguments.nid }}', $data['display']['default']['display_options']['footer']['area']['content']['value']); $this->assertEqual("Displaying @start - @end of @total\n\n{{ arguments.nid }}-result-{{ raw_arguments.nid }}", $data['display']['default']['display_options']['empty']['result']['content']); $this->assertEqual('{{ arguments.nid }}-title-{{ raw_arguments.nid }}', $data['display']['default']['display_options']['empty']['title']['title']); $this->assertEqual('{{ arguments.nid }}-entity-{{ raw_arguments.nid }}', $data['display']['default']['display_options']['empty']['entity_node']['target']); $this->assertEqual('{{ arguments.nid }} title {{ raw_arguments.nid }}', $data['display']['default']['display_options']['arguments']['nid']['title']); $this->assertEqual('{{ arguments.nid }} exception-title {{ raw_arguments.nid }}', $data['display']['default']['display_options']['arguments']['nid']['exception']['title']); $this->assertEqual('{{ arguments.nid }}-more-text-{{ raw_arguments.nid }}', $data['display']['default']['display_options']['use_more_text']); $this->assertEqual('{{ arguments.nid }}-custom-url-{{ raw_arguments.nid }}', $data['display']['default']['display_options']['link_url']); }
/** * Tests integration with image module. */ public function testImage() { /** @var \Drupal\image\ImageStyleInterface $style */ $style = ImageStyle::create(['name' => 'foo']); $style->save(); // Create a new image field 'bar' to be used in 'entity_test_fields' view. FieldStorageConfig::create(['entity_type' => 'entity_test', 'field_name' => 'bar', 'type' => 'image'])->save(); FieldConfig::create(['entity_type' => 'entity_test', 'bundle' => 'entity_test', 'field_name' => 'bar'])->save(); /** @var \Drupal\views\ViewEntityInterface $view */ $view = View::load('entity_test_fields'); $display =& $view->getDisplay('default'); // Add the 'bar' image field to 'entity_test_fields' view. $display['display_options']['fields']['bar'] = ['id' => 'bar', 'field' => 'bar', 'plugin_id' => 'field', 'table' => 'entity_test__bar', 'entity_type' => 'entity_test', 'entity_field' => 'bar', 'type' => 'image', 'settings' => ['image_style' => 'foo', 'image_link' => '']]; $view->save(); $dependencies = $view->getDependencies() + ['config' => []]; // Checks that style 'foo' is a dependency of view 'entity_test_fields'. $this->assertTrue(in_array('image.style.foo', $dependencies['config'])); // Delete the 'foo' image style. $style->delete(); // Checks that the view has been deleted too. $this->assertNull(View::load('entity_test_fields')); }
/** * Tests the title area handler. */ public function testTitleText() { // Confirm that the view has the normal title before making the view return // no result. $this->drupalGet('test-area-title'); $this->assertTitle('test_title_header | Drupal'); // Change the view to return no result. /** @var \Drupal\views\Entity\View $view */ $view = View::load('test_area_title'); $display =& $view->getDisplay('default'); $display['display_options']['filters']['name'] = ['field' => 'name', 'id' => 'name', 'table' => 'views_test_data', 'relationship' => 'none', 'plugin_id' => 'string', 'value' => 'Euler']; $view->save(); $this->drupalGet('test-area-title'); $this->assertTitle('test_title_empty | Drupal'); // Change the view to return a result instead. /** @var \Drupal\views\Entity\View $view */ $view = View::load('test_area_title'); $display =& $view->getDisplay('default'); $display['display_options']['filters']['name'] = ['field' => 'name', 'id' => 'name', 'table' => 'views_test_data', 'relationship' => 'none', 'plugin_id' => 'string', 'value' => 'Ringo']; $view->save(); $this->drupalGet('test-area-title'); $this->assertTitle('test_title_header | Drupal'); }
/** * @covers ::buildResponse */ public function testBuildResponse() { /** @var \Drupal\views\Entity\View $view */ $view = View::load('test_serializer_display_entity'); $display =& $view->getDisplay('rest_export_1'); $display['display_options']['defaults']['style'] = FALSE; $display['display_options']['style']['type'] = 'serializer'; $display['display_options']['style']['options']['formats'] = ['json', 'xml']; $view->save(); // No custom header should be set yet. $response = RestExport::buildResponse('test_serializer_display_entity', 'rest_export_1', []); $this->assertFalse($response->headers->get('Custom-Header')); // Clear render cache. /** @var \Drupal\Core\Cache\MemoryBackend $render_cache */ $render_cache = $this->container->get('cache_factory')->get('render'); $render_cache->deleteAll(); // A custom header should now be added. // @see rest_test_views_views_post_execute() $header = $this->randomString(); $this->container->get('state')->set('rest_test_views_set_header', $header); $response = RestExport::buildResponse('test_serializer_display_entity', 'rest_export_1', []); $this->assertEquals($header, $response->headers->get('Custom-Header')); }
/** * Test table fields in columns. */ public function testFieldInColumns() { $this->drupalGet('test-table'); // Ensure that both columns are in separate tds. // Check for class " views-field-job ", because just "views-field-job" won't // do: "views-field-job-1" would also contain "views-field-job". // @see Drupal\system\Tests\Form\ElementTest::testButtonClasses(). $result = $this->xpath('//tbody/tr/td[contains(concat(" ", @class, " "), " views-field-job ")]'); $this->assertTrue(count($result), 'Ensure there is a td with the class views-field-job'); $result = $this->xpath('//tbody/tr/td[contains(concat(" ", @class, " "), " views-field-job-1 ")]'); $this->assertTrue(count($result), 'Ensure there is a td with the class views-field-job-1'); // Combine the second job-column with the first one, with ', ' as separator. $view = View::load('test_table'); $display =& $view->getDisplay('default'); $display['display_options']['style']['options']['columns']['job_1'] = 'job'; $display['display_options']['style']['options']['info']['job']['separator'] = ', '; $view->save(); // Ensure that both columns are properly combined. $this->drupalGet('test-table'); $result = $this->xpath('//tbody/tr/td[contains(concat(" ", @class, " "), " views-field-job views-field-job-1 ")]'); $this->assertTrue(count($result), 'Ensure that the job column class names are joined into a single column'); $result = $this->xpath('//tbody/tr/td[contains(., "Drummer, Drummer")]'); $this->assertTrue(count($result), 'Ensure the job column values are joined into a single column'); }
/** * Ensures that saving a view calculates the cache contexts. */ public function testViewAddCacheMetadata() { $view = View::load('test_display'); $view->save(); $this->assertEqual(['languages', 'user', 'user.node_grants:view'], $view->getDisplay('default')['cache_metadata']['contexts']); }
/** * Tests adding, saving, and loading displays on configuration entities. */ protected function displayTests() { // Check whether a display can be added and saved to a View. $view = View::load('test_view_storage_new'); $new_id = $view->addDisplay('page', 'Test', 'test'); $display = $view->get('display'); // Ensure the right display_plugin is created/instantiated. $this->assertEqual($display[$new_id]['display_plugin'], 'page', 'New page display "test" uses the right display plugin.'); $executable = $view->getExecutable(); $executable->initDisplay(); $this->assertTrue($executable->displayHandlers->get($new_id) instanceof Page, 'New page display "test" uses the right display plugin.'); // To save this with a new ID, we should use createDuplicate(). $view = $view->createDuplicate(); $view->set('id', 'test_view_storage_new_new2'); $view->save(); $values = $this->config('views.view.test_view_storage_new_new2')->get(); $this->assertTrue(isset($values['display']['test']) && is_array($values['display']['test']), 'New display was saved.'); }
/** * Tests the view config entity */ public function testView() { $this->installConfig(['system', 'tmgmt']); $job = tmgmt_job_create('en', 'de'); $job->translator = 'test_translator'; $job->save(); $job_item = tmgmt_job_item_create('config', 'view', 'views.view.tmgmt_job_overview', array('tjid' => $job->id())); $job_item->save(); $view = View::load('tmgmt_job_overview'); $source_plugin = $this->container->get('plugin.manager.tmgmt.source')->createInstance('config'); $data = $source_plugin->getData($job_item); // Test the name property. $this->assertEqual($data['label']['#label'], 'Label'); $this->assertEqual($data['label']['#text'], $view->label()); $this->assertEqual($data['label']['#translate'], TRUE); $this->assertEqual($data['description']['#label'], 'Administrative description'); $this->assertEqual($data['description']['#text'], 'Gives a bulk operation overview of translation jobs in the system.'); $this->assertEqual($data['description']['#translate'], TRUE); $this->assertEqual($data['display']['default']['display_title']['#text'], 'Master'); $this->assertEqual($data['display']['default']['display_options']['exposed_form']['options']['submit_button']['#label'], 'Submit button text'); $this->assertEqual($data['display']['default']['display_options']['pager']['options']['expose']['items_per_page_label']['#label'], 'Items per page label'); // Tests for labels on more levels. $this->assertEqual($data['display']['default']['display_options']['pager']['options']['expose']['#label'], 'Exposed options'); $this->assertEqual($data['display']['default']['display_options']['pager']['options']['#label'], 'Paged output, full pager'); $this->assertEqual($data['display']['default']['display_options']['pager']['#label'], 'Pager'); $this->assertEqual($data['display']['default']['display_options']['#label'], 'Default display options'); $this->assertEqual($data['display']['default']['#label'], 'Display settings'); // Test item types. $this->assertEqual($source_plugin->getItemTypes()['view'], t('View')); // Now request a translation and save it back. $job->requestTranslation(); $items = $job->getItems(); $item = reset($items); $item->acceptTranslation(); $data = $item->getData(); // Check that the translations were saved correctly. $language_manager = \Drupal::languageManager(); $language_manager->setConfigOverrideLanguage($language_manager->getLanguage('de')); $view = View::load('tmgmt_job_overview'); $this->assertEqual($view->label(), $data['label']['#translation']['#text']); $this->assertEqual($view->get('description'), $data['description']['#translation']['#text']); $display = $view->get('display'); $this->assertEqual($display['default']['display_options']['title'], $data['label']['#translation']['#text']); $this->assertEqual($display['default']['display_options']['exposed_form']['options']['submit_button'], $data['display']['default']['display_options']['exposed_form']['options']['submit_button']['#translation']['#text']); }
/** * Test the node type for a single checkout. */ function testViewTranslateTabSingleCheckout() { $this->loginAsTranslator(array('translate configuration')); // Go to the translate tab. $this->drupalGet('admin/structure/views/view/content/translate'); // Assert some basic strings on that page. $this->assertText(t('Translations of Content view')); $this->assertText(t('There are 0 items in the translation cart.')); // Request a translation for german. $edit = array('languages[de]' => TRUE); $this->drupalPostForm(NULL, $edit, t('Request translation')); // Verify that we are on the translate tab. $this->assertText(t('One job needs to be checked out.')); $this->assertText('Content view (English to German, Unprocessed)'); // Submit. $this->drupalPostForm(NULL, array(), t('Submit to provider')); // Make sure that we're back on the originally defined destination URL. $this->assertUrl('admin/structure/views/view/content/translate'); // We are redirected back to the correct page. $this->drupalGet('admin/structure/views/view/content/translate'); // Translated languages should now be listed as Needs review. $rows = $this->xpath('//tbody/tr'); foreach ($rows as $value) { if ('German' == (string) $value->td[1]) { $this->assertEqual($value->td[2]->a, 'Needs review'); } } // Verify that the pending translation is shown. $this->clickLink(t('Needs review')); $this->drupalPostForm(NULL, array(), t('Save')); // Request a spanish translation. $edit = array('languages[es]' => TRUE); $this->drupalPostForm(NULL, $edit, t('Request translation')); // Verify that we are on the checkout page. $this->assertText(t('One job needs to be checked out.')); $this->assertText('Content view (English to Spanish, Unprocessed)'); $this->drupalPostForm(NULL, array(), t('Submit to provider')); // Make sure that we're back on the originally defined destination URL. $this->assertUrl('admin/structure/views/view/content/translate'); // Translated languages should now be listed as Needs review. $rows = $this->xpath('//tbody/tr'); $counter = 0; foreach ($rows as $value) { if ('Spanish' == (string) $value->td[1] || 'German' == (string) $value->td[1]) { $this->assertEqual($value->td[2]->a, 'Needs review'); $counter++; } } $this->assertEqual($counter, 2); // Test that a job can not be accepted if the entity does not exist. $this->clickLink(t('Needs review')); // Delete the view and assert that the job can not be accepted. $view_content = View::load('content'); $view_content->delete(); $this->drupalPostForm(NULL, array(), t('Save as completed')); $this->assertText(t('@id of type @type does not exist, the job can not be completed.', array('@id' => $view_content->id(), '@type' => $view_content->getEntityTypeId()))); }
/** * Tests that changing the route of a shortcut link works. */ public function testShortcutLinkChangeRoute() { $this->drupalLogin($this->rootUser); $this->drupalGet('admin/content'); $this->assertResponse(200); // Disable the view. View::load('content')->disable()->save(); /** @var \Drupal\Core\Routing\RouteBuilderInterface $router_builder */ $router_builder = \Drupal::service('router.builder'); $router_builder->rebuildIfNeeded(); $this->drupalGet('admin/content'); $this->assertResponse(200); }
/** * Get select options for Views displays that support Calendar with set granularity. * * @param $granularity * * @return array */ protected function viewOptionsForGranularity($granularity) { $options = []; $view_displays = Views::getApplicableViews('uses_route'); foreach ($view_displays as $view_display) { list($view_id, $display_id) = $view_display; $view = View::load($view_id); $view_exec = $view->getExecutable(); if ($argument = CalendarHelper::getDateArgumentHandler($view_exec, $display_id)) { if ($argument->getGranularity() == $granularity) { $route_name = CalendarHelper::getDisplayRouteName($view_id, $display_id); $options[$route_name] = $view->label() . ' : ' . $view_exec->displayHandlers->get($display_id)->display['display_title']; } } } return $options; }
/** * Ensures that saving a view calculates the cache contexts. */ public function testViewAddCacheMetadata() { $view = View::load('test_display'); $view->save(); $this->assertEqual(['languages:' . LanguageInterface::TYPE_CONTENT, 'languages:' . LanguageInterface::TYPE_INTERFACE, 'url.query_args', 'user.node_grants:view', 'user.permissions'], $view->getDisplay('default')['cache_metadata']['contexts']); }
/** * {@inheritdoc} */ public static function load($id) { return View::load($id); }
/** * Ensures that no XSS is possible for buttons. */ public function testDisplayTitleInButtonsXss() { $xss_markup = '"><script>alert(123)</script>'; $view = $this->randomView(); $view = View::load($view['id']); \Drupal::configFactory()->getEditable('views.settings')->set('ui.show.master_display', TRUE)->save(); foreach ([$xss_markup, '"><script>alert(123)</script>'] as $input) { $display =& $view->getDisplay('page_1'); $display['display_title'] = $input; $view->save(); $this->drupalGet("admin/structure/views/view/{$view->id()}"); $escaped = views_ui_truncate($input, 25); $this->assertEscaped($escaped); $this->assertNoRaw($xss_markup); $this->drupalGet("admin/structure/views/view/{$view->id()}/edit/page_1"); $this->assertEscaped("View {$escaped}"); $this->assertNoRaw("View {$xss_markup}"); $this->assertEscaped("Duplicate {$escaped}"); $this->assertNoRaw("Duplicate {$xss_markup}"); $this->assertEscaped("Delete {$escaped}"); $this->assertNoRaw("Delete {$xss_markup}"); } }
/** * Tests the calculation of the rendered dependencies. */ public function doTestCalculateDependencies() { $view = View::load('test_entity_area'); $dependencies = $view->calculateDependencies()->getDependencies(); // Ensure that both config and content entity dependencies are calculated. $this->assertEqual(['config' => ['block.block.test_block'], 'content' => ['entity_test:entity_test:aa0c61cb-b7bb-4795-972a-493dabcf529c']], $dependencies); }
/** * Tests the admin interface of exposed filter and sort items. */ function testExposedAdminUi() { $edit = array(); $this->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type'); // Be sure that the button is called exposed. $this->helperButtonHasLabel('edit-options-expose-button-button', t('Expose filter')); // The first time the filter UI is displayed, the operator and the // value forms should be shown. $this->assertFieldById('edit-options-operator-in', '', 'Operator In exists'); $this->assertFieldById('edit-options-operator-not-in', '', 'Operator Not In exists'); $this->assertFieldById('edit-options-value-page', '', 'Checkbox for Page exists'); $this->assertFieldById('edit-options-value-article', '', 'Checkbox for Article exists'); // Click the Expose filter button. $this->drupalPostForm('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type', $edit, t('Expose filter')); // Check the label of the expose button. $this->helperButtonHasLabel('edit-options-expose-button-button', t('Hide filter')); // Check the label of the grouped exposed button $this->helperButtonHasLabel('edit-options-group-button-button', t('Grouped filters')); // After exposing the filter, Operator and Value should be still here. $this->assertFieldById('edit-options-operator-in', '', 'Operator In exists'); $this->assertFieldById('edit-options-operator-not-in', '', 'Operator Not In exists'); $this->assertFieldById('edit-options-value-page', '', 'Checkbox for Page exists'); $this->assertFieldById('edit-options-value-article', '', 'Checkbox for Article exists'); // Check the validations of the filter handler. $edit = array(); $edit['options[expose][identifier]'] = ''; $this->drupalPostForm(NULL, $edit, t('Apply')); $this->assertText(t('The identifier is required if the filter is exposed.')); $edit = array(); $edit['options[expose][identifier]'] = 'value'; $this->drupalPostForm(NULL, $edit, t('Apply')); $this->assertText(t('This identifier is not allowed.')); // Now check the sort criteria. $this->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/sort/created'); $this->helperButtonHasLabel('edit-options-expose-button-button', t('Expose sort')); $this->assertNoFieldById('edit-options-expose-label', '', 'Make sure no label field is shown'); // Click the Grouped Filters button. $this->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type'); $this->drupalPostForm(NULL, array(), t('Grouped filters')); // After click on 'Grouped Filters', the standard operator and value should // not be displayed. $this->assertNoFieldById('edit-options-operator-in', '', 'Operator In not exists'); $this->assertNoFieldById('edit-options-operator-not-in', '', 'Operator Not In not exists'); $this->assertNoFieldById('edit-options-value-page', '', 'Checkbox for Page not exists'); $this->assertNoFieldById('edit-options-value-article', '', 'Checkbox for Article not exists'); // Check that after click on 'Grouped Filters', a new button is shown to // add more items to the list. $this->helperButtonHasLabel('edit-options-group-info-add-group', t('Add another item')); // Create a grouped filter $this->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type'); $edit = array(); $edit["options[group_info][group_items][1][title]"] = 'Is Article'; $edit["options[group_info][group_items][1][value][article]"] = 'article'; $edit["options[group_info][group_items][2][title]"] = 'Is Page'; $edit["options[group_info][group_items][2][value][page]"] = TRUE; $edit["options[group_info][group_items][3][title]"] = 'Is Page and Article'; $edit["options[group_info][group_items][3][value][article]"] = TRUE; $edit["options[group_info][group_items][3][value][page]"] = TRUE; $this->drupalPostForm(NULL, $edit, t('Apply')); // Select the empty operator, so the empty value should not trigger a form // error. $this->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/body_value'); $edit = array(); $edit["options[group_info][group_items][1][title]"] = $this->randomMachineName(); $edit["options[group_info][group_items][1][operator]"] = 'empty'; $this->drupalPostForm(NULL, $edit, t('Apply')); $this->assertUrl('admin/structure/views/view/test_exposed_admin_ui/edit/default', array(), 'Validation did not run for the empty operator.'); // Test the validation error message text is not shown. $this->assertNoText(t('The value is required if title for this item is defined.')); // Validate that all the titles are defined for each group $this->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type'); $edit = array(); $edit["options[group_info][group_items][1][title]"] = 'Is Article'; $edit["options[group_info][group_items][1][value][article]"] = TRUE; // This should trigger an error $edit["options[group_info][group_items][2][title]"] = ''; $edit["options[group_info][group_items][2][value][page]"] = TRUE; $edit["options[group_info][group_items][3][title]"] = 'Is Page and Article'; $edit["options[group_info][group_items][3][value][article]"] = TRUE; $edit["options[group_info][group_items][3][value][page]"] = TRUE; $this->drupalPostForm(NULL, $edit, t('Apply')); $this->assertRaw(t('The title is required if value for this item is defined.'), 'Group items should have a title'); // Un-expose the filter. $this->drupalGet('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/filter/type'); $this->drupalPostForm(NULL, array(), t('Hide filter')); // After Un-exposing the filter, Operator and Value should be shown again. $this->assertFieldById('edit-options-operator-in', '', 'Operator In exists after hide filter'); $this->assertFieldById('edit-options-operator-not-in', '', 'Operator Not In exists after hide filter'); $this->assertFieldById('edit-options-value-page', '', 'Checkbox for Page exists after hide filter'); $this->assertFieldById('edit-options-value-article', '', 'Checkbox for Article exists after hide filter'); // Click the Expose sort button. $edit = array(); $this->drupalPostForm('admin/structure/views/nojs/handler/test_exposed_admin_ui/default/sort/created', $edit, t('Expose sort')); // Check the label of the expose button. $this->helperButtonHasLabel('edit-options-expose-button-button', t('Hide sort')); $this->assertFieldById('edit-options-expose-label', '', 'Make sure a label field is shown'); // Test adding a new exposed sort criteria. $view_id = $this->randomView()['id']; $this->drupalGet("admin/structure/views/nojs/add-handler/{$view_id}/default/sort"); $this->drupalPostForm(NULL, ['name[node_field_data.created]' => 1], t('Add and configure @handler', ['@handler' => t('sort criteria')])); $this->assertFieldByXPath('//input[@name="options[order]" and @checked="checked"]', 'ASC', 'The default order is set.'); // Change the order and expose the sort. $this->drupalPostForm(NULL, ['options[order]' => 'DESC'], t('Apply')); $this->drupalPostForm("admin/structure/views/nojs/handler/{$view_id}/default/sort/created", [], t('Expose sort')); $this->assertFieldByXPath('//input[@name="options[order]" and @checked="checked"]', 'DESC'); $this->assertFieldByName('options[expose][label]', 'Authored on', 'The default label is set.'); // Change the label and save the view. $edit = ['options[expose][label]' => $this->randomString()]; $this->drupalPostForm(NULL, $edit, t('Apply')); $this->drupalPostForm(NULL, [], t('Save')); // Check that the values were saved. $display = View::load($view_id)->getDisplay('default'); $this->assertTrue($display['display_options']['sorts']['created']['exposed']); $this->assertEqual($display['display_options']['sorts']['created']['expose'], ['label' => $edit['options[expose][label]']]); $this->assertEqual($display['display_options']['sorts']['created']['order'], 'DESC'); }
/** * Tests REST export with views render caching enabled. */ public function testRestRenderCaching() { $this->drupalLogin($this->adminUser); /** @var \Drupal\Core\Render\RenderCacheInterface $render_cache */ $render_cache = \Drupal::service('render_cache'); // Enable render caching for the views. /** @var \Drupal\views\ViewEntityInterface $storage */ $storage = View::load('test_serializer_display_entity'); $options =& $storage->getDisplay('default'); $options['display_options']['cache'] = ['type' => 'tag']; $storage->save(); $original = DisplayPluginBase::buildBasicRenderable('test_serializer_display_entity', 'rest_export_1'); // Ensure that there is no corresponding render cache item yet. $original['#cache'] += ['contexts' => []]; $original['#cache']['contexts'] = Cache::mergeContexts($original['#cache']['contexts'], $this->container->getParameter('renderer.config')['required_cache_contexts']); $cache_tags = ['config:views.view.test_serializer_display_entity', 'entity_test:1', 'entity_test:10', 'entity_test:2', 'entity_test:3', 'entity_test:4', 'entity_test:5', 'entity_test:6', 'entity_test:7', 'entity_test:8', 'entity_test:9', 'entity_test_list']; $cache_contexts = ['entity_test_view_grants', 'languages:language_interface', 'theme', 'request_format']; $this->assertFalse($render_cache->get($original)); // Request the page, once in XML and once in JSON to ensure that the caching // varies by it. $result1 = $this->drupalGetJSON('test/serialize/entity'); $this->addRequestWithFormat('json'); $this->assertHeader('content-type', 'application/json'); $this->assertCacheContexts($cache_contexts); $this->assertCacheTags($cache_tags); $this->assertTrue($render_cache->get($original)); $result_xml = $this->drupalGetWithFormat('test/serialize/entity', 'xml'); $this->addRequestWithFormat('xml'); $this->assertHeader('content-type', 'text/xml; charset=UTF-8'); $this->assertCacheContexts($cache_contexts); $this->assertCacheTags($cache_tags); $this->assertTrue($render_cache->get($original)); // Ensure that the XML output is different from the JSON one. $this->assertNotEqual($result1, $result_xml); // Ensure that the cached page works. $result2 = $this->drupalGetJSON('test/serialize/entity'); $this->addRequestWithFormat('json'); $this->assertHeader('content-type', 'application/json'); $this->assertEqual($result2, $result1); $this->assertCacheContexts($cache_contexts); $this->assertCacheTags($cache_tags); $this->assertTrue($render_cache->get($original)); // Create a new entity and ensure that the cache tags are taken over. EntityTest::create(['name' => 'test_11', 'user_id' => $this->adminUser->id()])->save(); $result3 = $this->drupalGetJSON('test/serialize/entity'); $this->addRequestWithFormat('json'); $this->assertHeader('content-type', 'application/json'); $this->assertNotEqual($result3, $result2); // Add the new entity cache tag and remove the first one, because we just // show 10 items in total. $cache_tags[] = 'entity_test:11'; unset($cache_tags[array_search('entity_test:1', $cache_tags)]); $this->assertCacheContexts($cache_contexts); $this->assertCacheTags($cache_tags); $this->assertTrue($render_cache->get($original)); }
/** * Tests the various testcases of empty block rendering. */ public function testBlockEmptyRendering() { $url = new Url('test_page_test.test_page'); // Remove all views_test_data entries. \Drupal::database()->truncate('views_test_data')->execute(); /** @var \Drupal\views\ViewEntityInterface $view */ $view = View::load('test_view_block'); $view->invalidateCaches(); $block = $this->drupalPlaceBlock('views_block:test_view_block-block_1', array('label' => 'test_view_block-block_1:1', 'views_label' => 'Custom title')); $this->drupalGet(''); $this->assertEqual(1, count($this->xpath('//div[contains(@class, "block-views-blocktest-view-block-block-1")]'))); $display =& $view->getDisplay('block_1'); $display['display_options']['block_hide_empty'] = TRUE; $view->save(); $this->drupalGet($url); $this->assertEqual(0, count($this->xpath('//div[contains(@class, "block-views-blocktest-view-block-block-1")]'))); // Ensure that the view cachability metadata is propagated even, for an // empty block. $this->assertCacheTags(array_merge($block->getCacheTags(), ['block_view', 'config:block_list', 'config:views.view.test_view_block', 'rendered'])); $this->assertCacheContexts(['url.query_args:_wrapper_format']); // Add a header displayed on empty result. $display =& $view->getDisplay('block_1'); $display['display_options']['defaults']['header'] = FALSE; $display['display_options']['header']['example'] = ['field' => 'area_text_custom', 'id' => 'area_text_custom', 'table' => 'views', 'plugin_id' => 'text_custom', 'content' => 'test header', 'empty' => TRUE]; $view->save(); $this->drupalGet($url); $this->assertEqual(1, count($this->xpath('//div[contains(@class, "block-views-blocktest-view-block-block-1")]'))); $this->assertCacheTags(array_merge($block->getCacheTags(), ['block_view', 'config:block_list', 'config:views.view.test_view_block', 'rendered'])); $this->assertCacheContexts(['url.query_args:_wrapper_format']); // Hide the header on empty results. $display =& $view->getDisplay('block_1'); $display['display_options']['defaults']['header'] = FALSE; $display['display_options']['header']['example'] = ['field' => 'area_text_custom', 'id' => 'area_text_custom', 'table' => 'views', 'plugin_id' => 'text_custom', 'content' => 'test header', 'empty' => FALSE]; $view->save(); $this->drupalGet($url); $this->assertEqual(0, count($this->xpath('//div[contains(@class, "block-views-blocktest-view-block-block-1")]'))); $this->assertCacheTags(array_merge($block->getCacheTags(), ['block_view', 'config:block_list', 'config:views.view.test_view_block', 'rendered'])); $this->assertCacheContexts(['url.query_args:_wrapper_format']); // Add an empty text. $display =& $view->getDisplay('block_1'); $display['display_options']['defaults']['empty'] = FALSE; $display['display_options']['empty']['example'] = ['field' => 'area_text_custom', 'id' => 'area_text_custom', 'table' => 'views', 'plugin_id' => 'text_custom', 'content' => 'test empty']; $view->save(); $this->drupalGet($url); $this->assertEqual(1, count($this->xpath('//div[contains(@class, "block-views-blocktest-view-block-block-1")]'))); $this->assertCacheTags(array_merge($block->getCacheTags(), ['block_view', 'config:block_list', 'config:views.view.test_view_block', 'rendered'])); $this->assertCacheContexts(['url.query_args:_wrapper_format']); }