/** * {@inheritdoc}. */ public function execute() { parent::execute(); $render = ['view' => $this->view->render()]; $this->handleForm($render); return $render; }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->entityManager = $this->getMock('Drupal\\Core\\Entity\\EntityManagerInterface'); $this->entityStorage = $this->getMock('Drupal\\Core\\Entity\\EntityStorageInterface'); $this->entityViewBuilder = $this->getMock('Drupal\\Core\\Entity\\EntityViewBuilderInterface'); $this->executable = $this->getMockBuilder('Drupal\\views\\ViewExecutable')->disableOriginalConstructor()->getMock(); $this->display = $this->getMockBuilder('Drupal\\views\\Plugin\\views\\display\\DisplayPluginBase')->disableOriginalConstructor()->getMock(); $this->stylePlugin = $this->getMockBuilder('Drupal\\views\\Plugin\\views\\style\\StylePluginBase')->disableOriginalConstructor()->getMock(); $this->executable->style_plugin = $this->stylePlugin; $this->entityHandler = new Entity(array(), 'entity', array('entity_type' => 'entity_test'), $this->entityManager); $this->display->expects($this->any())->method('getPlugin')->with('style')->willReturn($this->stylePlugin); $this->executable->expects($this->any())->method('getStyle')->willReturn($this->stylePlugin); $token = $this->getMockBuilder('Drupal\\Core\\Utility\\Token')->disableOriginalConstructor()->getMock(); $token->expects($this->any())->method('replace')->willReturnArgument(0); $container = new ContainerBuilder(); $container->set('token', $token); \Drupal::setContainer($container); }
/** * @covers ::getRenderTokens */ public function testGetRenderTokensWithArguments() { $field = $this->setupTestField(['id' => 'id']); $field->view->args = ['argument value']; $field->view->build_info['substitutions']['{{ arguments.name }}'] = 'argument value'; $argument = $this->getMockBuilder('\\Drupal\\views\\Plugin\\views\\argument\\ArgumentPluginBase')->disableOriginalConstructor()->getMock(); $field->last_render = 'last rendered output'; $this->display->expects($this->any())->method('getHandlers')->willReturnMap([['argument', ['name' => $argument]], ['field', ['id' => $field]]]); $expected = ['{{ id }}' => 'last rendered output', '{{ arguments.name }}' => 'argument value', '{{ raw_arguments.name }}' => 'argument value']; $this->assertEquals($expected, $field->getRenderTokens([])); }
/** * Tests access on render caching. */ public function testRenderCaching() { $view = Views::getView('test_access_perm'); $display =& $view->storage->getDisplay('default'); $display['display_options']['cache'] = ['type' => 'tag']; /** @var \Drupal\Core\Render\RendererInterface $renderer */ $renderer = \Drupal::service('renderer'); /** @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */ $account_switcher = \Drupal::service('account_switcher'); // First access as user without access. $build = DisplayPluginBase::buildBasicRenderable('test_access_perm', 'default'); $account_switcher->switchTo($this->normalUser); $result = $renderer->renderPlain($build); $this->assertNotEqual($result, ''); // Then with access. $build = DisplayPluginBase::buildBasicRenderable('test_access_perm', 'default'); $account_switcher->switchTo($this->webUser); $result = $renderer->renderPlain($build); $this->assertEqual($result, ''); }
/** * Tests the output caching on an actual page. */ public function testCacheOutputOnPage() { $view = Views::getView('test_display'); $view->storage->setStatus(TRUE); $view->setDisplay('page_1'); $view->display_handler->overrideOption('cache', array('type' => 'time', 'options' => array('results_lifespan' => '3600', 'output_lifespan' => '3600'))); $view->save(); $this->container->get('router.builder')->rebuildIfNeeded(); /** @var \Drupal\Core\Render\RenderCacheInterface $render_cache */ $render_cache = \Drupal::service('render_cache'); $cache_element = DisplayPluginBase::buildBasicRenderable('test_display', 'page_1'); $cache_element['#cache'] += ['contexts' => $this->container->getParameter('renderer.config')['required_cache_contexts']]; $this->assertFalse($render_cache->get($cache_element)); $this->drupalGet('test-display'); $this->assertResponse(200); $this->assertTrue($render_cache->get($cache_element)); $cache_tags = ['config:user.role.anonymous', 'config:views.view.test_display', 'node_list', 'rendered']; $this->assertCacheTags($cache_tags); $this->drupalGet('test-display'); $this->assertResponse(200); $this->assertTrue($render_cache->get($cache_element)); $this->assertCacheTags($cache_tags); }
/** * Asserts a view's result & render cache items' cache tags. * * This method starts with a pre bubbling basic render array. * * @param \Drupal\views\ViewExecutable $view * The view. * @param string[] $expected_render_array_cache_tags * The expected render cache tags. * @param bool $views_caching_is_enabled * Defines whether views output / render caching is enabled. * * @return array * The render array. */ protected function assertViewsCacheTagsFromStaticRenderArray(ViewExecutable $view, array $expected_render_array_cache_tags, $views_caching_is_enabled) { $original = $build = DisplayPluginBase::buildBasicRenderable($view->id(), $view->current_display ?: 'default', $view->args); /** @var \Drupal\Core\Render\RendererInterface $renderer */ $renderer = \Drupal::service('renderer'); /** @var \Drupal\Core\Render\RenderCacheInterface $render_cache */ $render_cache = \Drupal::service('render_cache'); // Ensure the current request is a GET request so that render caching is // active for direct rendering of views, just like for actual requests. /** @var \Symfony\Component\HttpFoundation\RequestStack $request_stack */ $request_stack = \Drupal::service('request_stack'); $request = new Request(); $request->server->set('REQUEST_TIME', REQUEST_TIME); $request_stack->push($request); $renderer->renderRoot($build); // Render array cache tags. $this->pass('Checking render array cache tags.'); sort($expected_render_array_cache_tags); $this->assertEqual($build['#cache']['tags'], $expected_render_array_cache_tags); $this->debugCacheTags($build['#cache']['tags'], $expected_render_array_cache_tags); $this->pass('Checking Views render cache item cache tags.'); $original['#cache'] += ['contexts' => []]; $original['#cache']['contexts'] = Cache::mergeContexts($original['#cache']['contexts'], $this->container->getParameter('renderer.config')['required_cache_contexts']); $render_cache_item = $render_cache->get($original); if ($views_caching_is_enabled) { $this->assertTrue(!empty($render_cache_item), 'Render cache item found.'); if ($render_cache_item) { $this->assertEqual($render_cache_item['#cache']['tags'], $expected_render_array_cache_tags); $this->debugCacheTags($render_cache_item['#cache']['tags'], $expected_render_array_cache_tags); } } else { $this->assertFalse($render_cache_item, 'Render cache item not found.'); } return $build; }
/** * Tests access on render caching. */ public function testRenderCaching() { $view = Views::getView('test_access_role'); $display =& $view->storage->getDisplay('default'); $display['display_options']['cache'] = ['type' => 'tag']; $display['display_options']['access']['options']['role'] = array($this->normalRole => $this->normalRole); $view->save(); /** @var \Drupal\Core\Render\RendererInterface $renderer */ $renderer = \Drupal::service('renderer'); /** @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */ $account_switcher = \Drupal::service('account_switcher'); // First access as user with access. $build = DisplayPluginBase::buildBasicRenderable('test_access_role', 'default'); $account_switcher->switchTo($this->normalUser); $result = $renderer->renderPlain($build); $this->assertTrue(in_array('user.roles', $build['#cache']['contexts'])); $this->assertEqual(['config:views.view.test_access_role'], $build['#cache']['tags']); $this->assertEqual(Cache::PERMANENT, $build['#cache']['max-age']); $this->assertNotEqual($result, ''); // Then without access. $build = DisplayPluginBase::buildBasicRenderable('test_access_role', 'default'); $account_switcher->switchTo($this->webUser); $result = $renderer->renderPlain($build); // @todo Fix this in https://www.drupal.org/node/2551037, // DisplayPluginBase::applyDisplayCachablityMetadata() is not invoked when // using buildBasicRenderable() and a Views access plugin returns FALSE. //$this->assertTrue(in_array('user.roles', $build['#cache']['contexts'])); //$this->assertEqual([], $build['#cache']['tags']); $this->assertEqual(Cache::PERMANENT, $build['#cache']['max-age']); $this->assertEqual($result, ''); }
/** * {@inheritdoc} */ public function remove() { parent::remove(); if ($this->entityManager->hasDefinition('block')) { $plugin_id = 'views_block:' . $this->view->storage->id() . '-' . $this->display['id']; foreach ($this->entityManager->getStorage('block')->loadByProperties(['plugin' => $plugin_id]) as $block) { $block->delete(); } } }
/** * Sets options for a display, inheriting from the defaults when possible. * * This function can be used to set options for a display when it is desired * that the options inherit from the default display whenever possible. This * avoids setting too many options as overrides, which will be harder for the * user to modify later. For example, if $this->setDefaultOptions() was * previously called on a page display and then this function is called on a * block display, and if the user entered the same title for both displays in * the views wizard, then the view will wind up with the title stored as the * default (with the page and block both inheriting from it). * * @param array $options * An array whose keys are the name of each option and whose values are the * desired values to set. * @param \Drupal\views\View\plugin\display\DisplayPluginBase $display * The display handler which the options will be applied to. The default * display will actually be assigned the options (and this display will * inherit them) when possible. * @param \Drupal\views\View\plugin\display\DisplayPluginBase $default_display * The default display handler, which will store the options when possible. */ protected function setOverrideOptions(array $options, DisplayPluginBase $display, DisplayPluginBase $default_display) { foreach ($options as $option => $value) { // Only override the default value if it is different from the value that // was provided. $default_value = $default_display->getOption($option); if (!isset($default_value)) { $display->setOption($option, $value); } elseif ($default_value !== $value) { $display->overrideOption($option, $value); } } }
/** * Sets up a display with empty arguments and fields. */ protected function setupDisplayWithEmptyArgumentsAndFields() { $this->display->expects($this->any())->method('getHandlers')->willReturnMap([['argument', []], ['field', []]]); }
/** * Get the base path used for this view. */ public function getPath() { if (!empty($this->override_path)) { return $this->override_path; } if (empty($this->display_handler)) { if (!$this->setDisplay('default')) { return FALSE; } } return $this->display_handler->getPath(); }
/** * Attachment displays only use exposed widgets if * they are set to inherit the exposed filter settings * of their parent display. */ public function usesExposed() { if (!empty($this->options['inherit_exposed_filters']) && parent::usesExposed()) { return TRUE; } return FALSE; }
/** * {@inheritdoc} */ public function validate() { $errors = parent::validate(); $errors += $this->validatePath($this->getOption('path')); return $errors; }
/** * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::validate(). */ public function validate() { $errors = parent::validate(); // Verify that search fields are set up. $style = $this->getOption('style'); if (!isset($style['options']['search_fields'])) { $errors[] = $this->t('Display "@display" needs a selected search fields to work properly. See the settings for the Entity Reference list format.', array('@display' => $this->display['display_title'])); } else { // Verify that the search fields used actually exist. $fields = array_keys($this->handlers['field']); foreach ($style['options']['search_fields'] as $field_alias => $enabled) { if ($enabled && !in_array($field_alias, $fields)) { $errors[] = $this->t('Display "@display" uses field %field as search field, but the field is no longer present. See the settings for the Entity Reference list format.', array('@display' => $this->display['display_title'], '%field' => $field_alias)); } } } return $errors; }
/** * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::remove(). */ public function remove() { parent::remove(); $plugin_id = 'views_block:' . $this->view->storage->id() . '-' . $this->display['id']; foreach (entity_load_multiple_by_properties('block', array('plugin' => $plugin_id)) as $block) { $block->delete(); } }
/** * Tests random ordering with tags based caching. * * The random sorting should opt out of caching by defining a max age of 0. * At the same time, the row render caching still works. */ public function testRandomOrderingWithRenderCaching() { $view_random = $this->getBasicRandomView(); $display =& $view_random->storage->getDisplay('default'); $display['display_options']['cache'] = ['type' => 'tag']; $view_random->storage->save(); /** @var \Drupal\Core\Render\RendererInterface $renderer */ $renderer = \Drupal::service('renderer'); /** @var \Drupal\Core\Render\RenderCacheInterface $render_cache */ $render_cache = \Drupal::service('render_cache'); $original = $build = DisplayPluginBase::buildBasicRenderable($view_random->id(), 'default'); $result = $renderer->renderPlain($build); $original['#cache'] += ['contexts' => []]; $original['#cache']['contexts'] = Cache::mergeContexts($original['#cache']['contexts'], $this->container->getParameter('renderer.config')['required_cache_contexts']); $this->assertFalse($render_cache->get($original), 'Ensure there is no render cache entry.'); $build = DisplayPluginBase::buildBasicRenderable($view_random->id(), 'default'); $result2 = $renderer->renderPlain($build); // Ensure that the random ordering works and don't produce the same result. $this->assertNotEqual($result, $result2); }
/** * {@inheritdoc} */ public function validate() { $errors = parent::validate(); foreach ($this->view->displayHandlers as $display_handler) { $errors[] = 'error'; } return $errors; }
/** * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::submitOptionsForm(). */ public function submitOptionsForm(&$form, FormStateInterface $form_state) { parent::submitOptionsForm($form, $form_state); switch ($form_state->get('section')) { case 'test_option': $this->setOption('test_option', $form_state->getValue('test_option')); break; } }
/** * 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)); }
/** * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::submitOptionsForm(). */ public function submitOptionsForm(&$form, &$form_state) { parent::submitOptionsForm($form, $form_state); switch ($form_state['section']) { case 'test_option': $this->setOption('test_option', $form_state['values']['test_option']); break; } }