/** * Tests creating a node revision view. */ public function testViewAdd() { $this->drupalCreateContentType(array('type' => 'article')); // Create two nodes with two revision. $node_storage = \Drupal::entityManager()->getStorage('node'); /** @var \Drupal\node\NodeInterface $node */ $node = $node_storage->create(array('title' => $this->randomString(), 'type' => 'article', 'created' => REQUEST_TIME + 40)); $node->save(); $node = $node->createDuplicate(); $node->setNewRevision(); $node->created->value = REQUEST_TIME + 20; $node->save(); $node = $node_storage->create(array('title' => $this->randomString(), 'type' => 'article', 'created' => REQUEST_TIME + 30)); $node->save(); $node = $node->createDuplicate(); $node->setNewRevision(); $node->created->value = REQUEST_TIME + 10; $node->save(); $view = array(); $view['label'] = $this->randomMachineName(16); $view['id'] = strtolower($this->randomMachineName(16)); $view['description'] = $this->randomMachineName(16); $view['page[create]'] = FALSE; $view['show[wizard_key]'] = 'node_revision'; $this->drupalPostForm('admin/structure/views/add', $view, t('Save and edit')); $view_storage_controller = \Drupal::entityManager()->getStorage('view'); /** @var \Drupal\views\Entity\View $view */ $view = $view_storage_controller->load($view['id']); $this->assertEqual($view->get('base_table'), 'node_field_revision'); $executable = Views::executableFactory()->get($view); $this->executeView($executable); $this->assertIdenticalResultset($executable, array(array('vid' => 1), array('vid' => 3), array('vid' => 2), array('vid' => 4)), array('vid' => 'vid')); }
/** * Tests role access plugin. */ function testAccessRole() { /** @var \Drupal\views\ViewStorageInterface $view */ $view = \Drupal::entityManager()->getStorage('view')->load('test_access_role'); $display =& $view->getDisplay('default'); $display['display_options']['access']['options']['role'] = array($this->normalRole => $this->normalRole); $view->save(); $executable = Views::executableFactory()->get($view); $executable->setDisplay('page_1'); $access_plugin = $executable->display_handler->getPlugin('access'); $this->assertTrue($access_plugin instanceof Role, 'Make sure the right class got instantiated.'); // Test the access() method on the access plugin. $this->assertTrue($executable->display_handler->access($this->adminUser), 'Admin-Account should be able to access the view everytime'); $this->assertFalse($executable->display_handler->access($this->webUser)); $this->assertTrue($executable->display_handler->access($this->normalUser)); $this->drupalLogin($this->adminUser); $this->drupalGet('test-role'); $this->assertResponse(200); $this->drupalLogin($this->webUser); $this->drupalGet('test-role'); $this->assertResponse(403); $this->drupalLogin($this->normalUser); $this->drupalGet('test-role'); $this->assertResponse(200); }
/** * Tests role access plugin. */ function testAccessRole() { /** @var \Drupal\views\ViewEntityInterface $view */ $view = \Drupal::entityManager()->getStorage('view')->load('test_access_role'); $display =& $view->getDisplay('default'); $display['display_options']['access']['options']['role'] = array($this->normalRole => $this->normalRole); $view->save(); $this->container->get('router.builder')->rebuildIfNeeded(); $expected = ['config' => ['user.role.' . $this->normalRole], 'module' => ['user']]; $this->assertIdentical($expected, $view->calculateDependencies()->getDependencies()); $executable = Views::executableFactory()->get($view); $executable->setDisplay('page_1'); $access_plugin = $executable->display_handler->getPlugin('access'); $this->assertTrue($access_plugin instanceof Role, 'Make sure the right class got instantiated.'); // Test the access() method on the access plugin. $this->assertFalse($executable->display_handler->access($this->webUser)); $this->assertTrue($executable->display_handler->access($this->normalUser)); $this->drupalLogin($this->webUser); $this->drupalGet('test-role'); $this->assertResponse(403); $this->assertCacheContext('user.roles'); $this->drupalLogin($this->normalUser); $this->drupalGet('test-role'); $this->assertResponse(200); $this->assertCacheContext('user.roles'); // Test allowing multiple roles. $view = Views::getView('test_access_role')->storage; $display =& $view->getDisplay('default'); $display['display_options']['access']['options']['role'] = array($this->normalRole => $this->normalRole, 'anonymous' => 'anonymous'); $view->save(); $this->container->get('router.builder')->rebuildIfNeeded(); // Ensure that the list of roles is sorted correctly, if the generated role // ID comes before 'anonymous', see https://www.drupal.org/node/2398259. $roles = ['user.role.anonymous', 'user.role.' . $this->normalRole]; sort($roles); $expected = ['config' => $roles, 'module' => ['user']]; $this->assertIdentical($expected, $view->calculateDependencies()->getDependencies()); $this->drupalLogin($this->webUser); $this->drupalGet('test-role'); $this->assertResponse(403); $this->assertCacheContext('user.roles'); $this->drupalLogout(); $this->drupalGet('test-role'); $this->assertResponse(200); $this->assertCacheContext('user.roles'); $this->drupalLogin($this->normalUser); $this->drupalGet('test-role'); $this->assertResponse(200); $this->assertCacheContext('user.roles'); }
/** * Run attachment displays for the view. */ public function attachDisplays() { if (!empty($this->is_attachment)) { return; } if (!$this->display_handler->acceptAttachments()) { return; } $this->is_attachment = TRUE; // Find out which other displays attach to the current one. foreach ($this->display_handler->getAttachedDisplays() as $id) { $display_handler = $this->displayHandlers->get($id); // Only attach enabled attachments. if ($display_handler->isEnabled()) { $cloned_view = Views::executableFactory()->get($this->storage); $display_handler->attachTo($cloned_view, $this->current_display, $this->element); } } $this->is_attachment = FALSE; }
/** * Constructs a View UI object. * * @param \Drupal\views\ViewStorageInterface $storage * The View storage object to wrap. */ public function __construct(ViewStorageInterface $storage, ViewExecutable $executable = NULL) { $this->entityType = 'view'; $this->storage = $storage; if (!isset($executable)) { $executable = Views::executableFactory()->get($this); } $this->executable = $executable; }
/** * Gets an executable instance for this view. * * @return \Drupal\views\ViewExecutable * A view executable instance. */ public function getExecutable() { // Ensure that an executable View is available. if (!isset($this->executable)) { $this->executable = Views::executableFactory()->get($this); } return $this->executable; }
/** * {@inheritdoc} */ public static function preDelete(EntityStorageInterface $storage, array $entities) { parent::preDelete($storage, $entities); // Call the remove() hook on the individual displays. /** @var \Drupal\views\ViewEntityInterface $entity */ foreach ($entities as $entity) { $executable = Views::executableFactory()->get($entity); foreach ($entity->get('display') as $display_id => $display) { $executable->setDisplay($display_id); $executable->getDisplay()->remove(); } } }