/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->adminUser = $this->drupalCreateUser(['administer content types', 'administer blocks', 'administer comments', 'administer comment types', 'post comments', 'create article content', 'access administration pages', 'access comments', 'access content']); $this->drupalLogin($this->adminUser); $this->drupalPlaceBlock('local_tasks_block'); $this->node = Node::create(['type' => 'article', 'title' => 'New node', 'promote' => 1, 'uid' => $this->adminUser->id()]); $this->node->save(); }
/** * Tests the presence of the expected cache tag in various situations. */ function testSearchText() { $this->drupalLogin($this->searchingUser); // Initial page for searching nodes. $this->drupalGet('search/node'); $this->assertCacheTag('config:search.page.node_search'); $this->assertCacheTag('search_index:node_search'); $this->assertCacheTag('node_list'); // Node search results. $edit = array(); $edit['keys'] = 'bike shed'; $this->drupalPostForm('search/node', $edit, t('Search')); $this->assertText('bike shed shop'); $this->assertCacheTag('config:search.page.node_search'); $this->assertCacheTag('search_index'); $this->assertCacheTag('search_index:node_search'); $this->assertCacheTag('node:1'); $this->assertCacheTag('user:2'); $this->assertCacheTag('rendered'); $this->assertCacheTag('node_list'); // Updating a node should invalidate the search plugin's index cache tag. $this->node->title = 'bike shop'; $this->node->save(); $this->drupalPostForm('search/node', $edit, t('Search')); $this->assertText('bike shop'); $this->assertCacheTag('config:search.page.node_search'); $this->assertCacheTag('search_index'); $this->assertCacheTag('search_index:node_search'); $this->assertCacheTag('node:1'); $this->assertCacheTag('user:2'); $this->assertCacheTag('rendered'); $this->assertCacheTag('node_list'); // Deleting a node should invalidate the search plugin's index cache tag. $this->node->delete(); $this->drupalPostForm('search/node', $edit, t('Search')); $this->assertText('Your search yielded no results.'); $this->assertCacheTag('config:search.page.node_search'); $this->assertCacheTag('search_index'); $this->assertCacheTag('search_index:node_search'); $this->assertCacheTag('node_list'); // Initial page for searching users. $this->drupalGet('search/user'); $this->assertCacheTag('config:search.page.user_search'); $this->assertCacheTag('user_list'); $this->assertNoCacheTag('search_index'); $this->assertNoCacheTag('search_index:user_search'); // User search results. $edit['keys'] = $this->searchingUser->getUsername(); $this->drupalPostForm('search/user', $edit, t('Search')); $this->assertCacheTag('config:search.page.user_search'); $this->assertCacheTag('user_list'); $this->assertCacheTag('user:2'); $this->assertNoCacheTag('search_index'); $this->assertNoCacheTag('search_index:user_search'); }
/** * Creates a new revision for a given node. * * @param \Drupal\node\NodeInterface $node * A node object. * * @return \Drupal\node\NodeInterface * A node object with up to date revision information. */ protected function createNodeRevision(NodeInterface $node) { // Create revision with a random title and body and update variables. $node->title = $this->randomMachineName(); $node->body = array('value' => $this->randomMachineName(32), 'format' => filter_default_format()); $node->setNewRevision(); $node->save(); return $node; }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->installSchema('node', 'node_access'); $this->installEntitySchema('node'); $this->installEntitySchema('user'); $this->installEntitySchema('content_moderation_state'); $this->installConfig('content_moderation'); $node_type = NodeType::create(['type' => 'example']); $node_type->setThirdPartySetting('content_moderation', 'enabled', TRUE); $node_type->setThirdPartySetting('content_moderation', 'allowed_moderation_states', ['draft']); $node_type->setThirdPartySetting('content_moderation', 'default_moderation_state', 'draft'); $node_type->save(); $this->testNode = Node::create(['type' => 'example', 'title' => 'Test title']); $this->testNode->save(); \Drupal::entityTypeManager()->getStorage('node')->resetCache(); $this->testNode = Node::load($this->testNode->id()); }
/** * Simulates the scheduled (un)publication of a node. * * @param \Drupal\node\NodeInterface $node * The node to schedule. * @param string $action * The action to perform: either 'publish' or 'unpublish'. Defaults to * 'publish'. * * @return \Drupal\node\NodeInterface * The updated node, after scheduled (un)publication via a cron run. */ protected function schedule(NodeInterface $node, $action = 'publish') { $node_storage = $this->container->get('entity.manager')->getStorage('node'); // Simulate scheduling by setting the (un)publication date in the past and // running cron. $node->{$action . '_on'} = strtotime('-1 day'); $node->save(); scheduler_cron(); $node_storage->resetCache(array($node->id())); return $node_storage->load($node->id()); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { // The revision timestamp will be updated when the revision is saved. Keep // the original one for the confirmation message. $original_revision_timestamp = $this->revision->getRevisionCreationTime(); $this->revision = $this->prepareRevertedRevision($this->revision, $form_state); $this->revision->revision_log = t('Copy of the revision from %date.', ['%date' => $this->dateFormatter->format($original_revision_timestamp)]); $this->revision->save(); $this->logger('content')->notice('@type: reverted %title revision %revision.', ['@type' => $this->revision->bundle(), '%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]); drupal_set_message(t('@type %title has been reverted to the revision from %revision-date.', ['@type' => node_get_type_label($this->revision), '%title' => $this->revision->label(), '%revision-date' => $this->dateFormatter->format($original_revision_timestamp)])); $form_state->setRedirect('entity.node.version_history', array('node' => $this->revision->id())); }
/** * {@inheritdoc} */ public function submitForm(array &$form, array &$form_state) { $this->revision->setNewRevision(); // Make this the new default revision for the node. $this->revision->isDefaultRevision(TRUE); // The revision timestamp will be updated when the revision is saved. Keep the // original one for the confirmation message. $original_revision_timestamp = $this->revision->getRevisionCreationTime(); $this->revision->revision_log = t('Copy of the revision from %date.', array('%date' => format_date($original_revision_timestamp))); $this->revision->save(); watchdog('content', '@type: reverted %title revision %revision.', array('@type' => $this->revision->bundle(), '%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId())); drupal_set_message(t('@type %title has been reverted back to the revision from %revision-date.', array('@type' => node_get_type_label($this->revision), '%title' => $this->revision->label(), '%revision-date' => format_date($original_revision_timestamp)))); $form_state['redirect_route'] = array('route_name' => 'node.revision_overview', 'route_parameters' => array('node' => $this->revision->id())); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $this->revision->setNewRevision(); // Make this the new default revision for the node. $this->revision->isDefaultRevision(TRUE); // The revision timestamp will be updated when the revision is saved. Keep the // original one for the confirmation message. $original_revision_timestamp = $this->revision->getRevisionCreationTime(); $this->revision->revision_log = t('Copy of the revision from %date.', array('%date' => format_date($original_revision_timestamp))); $this->revision->save(); $this->logger('content')->notice('@type: reverted %title revision %revision.', array('@type' => $this->revision->bundle(), '%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId())); drupal_set_message(t('@type %title has been reverted to the revision from %revision-date.', array('@type' => node_get_type_label($this->revision), '%title' => $this->revision->label(), '%revision-date' => format_date($original_revision_timestamp)))); $form_state->setRedirect('entity.node.version_history', array('node' => $this->revision->id())); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); // Create a flag. $this->flag = $this->createFlag('node', ['article'], 'reload'); // Create a user who may flag and log them in. This ensures we don't have // to worry about flag access. $this->adminUser = $this->drupalCreateUser([ 'administer flags', // This permission is needed to change the view mode settings to show and // hide the flag link field. 'administer node display', ]); $this->grantFlagPermissions($this->flag); $this->drupalLogin($this->adminUser); // Create a node to flag. $this->node = Node::create([ 'body' => [ [ 'value' => $this->randomMachineName(32), 'format' => filter_default_format(), ], ], 'type' => 'article', 'title' => $this->randomMachineName(8), 'uid' => $this->adminUser->id(), 'status' => 1, // Promoted to front page to test teaser view mode. 'promote' => 1, 'sticky' => 0, ]); $this->node->save(); }
/** * @cover replacePlaceHolders */ public function testBubbleableMetadata() { // Make sure the bubbleable metadata added by the fetcher is properly passed // though. $bubbleable_metadata = new BubbleableMetadata(); // Save the node, so it gets a cache tag. $this->node->save(); $this->placeholderResolver->replacePlaceHolders('test {{node.field_integer}}', ['node' => $this->node->getTypedData()], $bubbleable_metadata); $expected = ['node:' . $this->node->id()]; $this->assertEquals($expected, $bubbleable_metadata->getCacheTags()); // Ensure cache tags of filters are added in. $bubbleable_metadata = new BubbleableMetadata(); $this->placeholderResolver->replacePlaceHolders("test {{ node.created.value | format_date('medium') }}", ['node' => $this->node->getTypedData()], $bubbleable_metadata); $expected = Cache::mergeTags(['node:' . $this->node->id()], DateFormat::load('medium')->getCacheTags()); $this->assertEquals($expected, $bubbleable_metadata->getCacheTags()); }
/** * @cover fetchDataByPropertyPath */ public function testBubbleableMetadata() { $this->node->field_integer->setValue([]); // Save the node, so that it gets an ID and it has a cache tag. $this->node->save(); // Also add a user for testing cache tags of references. $user = $this->entityTypeManager->getStorage('user')->create(['name' => 'test', 'type' => 'user']); $user->save(); $this->node->uid->entity = $user; $bubbleable_metadata = new BubbleableMetadata(); $this->dataFetcher->fetchDataByPropertyPath($this->node->getTypedData(), 'title.value', $bubbleable_metadata)->getValue(); $expected = ['node:' . $this->node->id()]; $this->assertEquals($expected, $bubbleable_metadata->getCacheTags()); // Test cache tags of references are added correctly. $this->dataFetcher->fetchDataByPropertyPath($this->node->getTypedData(), 'uid.entity.name', $bubbleable_metadata)->getValue(); $expected = ['node:' . $this->node->id(), 'user:' . $user->id()]; $this->assertEquals($expected, $bubbleable_metadata->getCacheTags()); }
/** * Tests the tag cache plugin. */ public function testTagCaching() { /** @var \Drupal\Core\Render\RendererInterface $renderer */ $renderer = \Drupal::service('renderer'); $view = Views::getView('test_tag_cache'); $build = $view->buildRenderable(); $renderer->renderPlain($build); // Saving the view should invalidate the tags. $cache_plugin = $view->display_handler->getPlugin('cache'); $this->assertTrue($cache_plugin->cacheGet('results'), 'Results cache found.'); $this->assertTrue($this->getRenderCache($view), 'Output cache found.'); $view->storage->save(); $this->assertFalse($cache_plugin->cacheGet('results'), 'Results cache empty after the view is saved.'); $this->assertFalse($this->getRenderCache($view), 'Output cache empty after the view is saved.'); $view->destroy(); $build = $view->buildRenderable(); $renderer->renderPlain($build); // Test invalidating the nodes in this view invalidates the cache. $cache_plugin = $view->display_handler->getPlugin('cache'); $this->assertTrue($cache_plugin->cacheGet('results'), 'Results cache found.'); $this->assertTrue($this->getRenderCache($view), 'Output cache found.'); $this->nodeViewBuilder->resetCache($this->pages); $this->assertFalse($cache_plugin->cacheGet('results'), 'Results cache empty after resetCache is called with pages.'); $this->assertFalse($this->getRenderCache($view), 'Output cache empty after resetCache is called with pages.'); $view->destroy(); $build = $view->buildRenderable(); $renderer->renderPlain($build); // Test saving a node in this view invalidates the cache. $cache_plugin = $view->display_handler->getPlugin('cache'); $this->assertTrue($cache_plugin->cacheGet('results'), 'Results cache found.'); $this->assertTrue($this->getRenderCache($view), 'Output cache found.'); $node = reset($this->pages); $node->save(); $this->assertFalse($cache_plugin->cacheGet('results'), 'Results cache empty after a page node is saved.'); $this->assertFalse($this->getRenderCache($view), 'Output cache empty after a page node is saved.'); $view->destroy(); $build = $view->buildRenderable(); $renderer->renderPlain($build); // Test saving a node not in this view invalidates the cache too. $cache_plugin = $view->display_handler->getPlugin('cache'); $this->assertTrue($cache_plugin->cacheGet('results'), 'Results cache found.'); $this->assertTrue($this->getRenderCache($view), 'Output cache found.'); $this->article->save(); $this->assertFalse($cache_plugin->cacheGet('results'), 'Results cache empty after an article node is saved.'); $this->assertFalse($this->getRenderCache($view), 'Output cache empty after an article node is saved.'); $view->destroy(); $build = $view->buildRenderable(); $renderer->renderPlain($build); // Test that invalidating a tag for a user, does not invalidate the cache, // as the user entity type will not be contained in the views cache tags. $cache_plugin = $view->display_handler->getPlugin('cache'); $this->assertTrue($cache_plugin->cacheGet('results'), 'Results cache found.'); $this->assertTrue($this->getRenderCache($view), 'Output cache found.'); $this->userViewBuilder->resetCache(array($this->user)); $cache_plugin = $view->display_handler->getPlugin('cache'); $this->assertTrue($cache_plugin->cacheGet('results'), 'Results cache found after a user is invalidated.'); $this->assertTrue($this->getRenderCache($view), 'Output cache found after a user is invalidated.'); $view->destroy(); // Invalidate the views cache tags in order to invalidate the render // caching. \Drupal::service('cache_tags.invalidator')->invalidateTags($view->storage->getCacheTagsToInvalidate()); $build = $view->buildRenderable(); $renderer->renderPlain($build); // Test the cacheFlush method invalidates the cache. $cache_plugin = $view->display_handler->getPlugin('cache'); $this->assertTrue($cache_plugin->cacheGet('results'), 'Results cache found.'); $this->assertTrue($this->getRenderCache($view), 'Output cache found.'); $cache_plugin->cacheFlush(); $cache_plugin = $view->display_handler->getPlugin('cache'); $this->assertFalse($cache_plugin->cacheGet('results'), 'Results cache empty after the cacheFlush() method is called.'); $this->assertFalse($this->getRenderCache($view), 'Output cache empty after the cacheFlush() method is called.'); }
/** * Creates a series of revisions for the specified node. * * @param \Drupal\node\NodeInterface $node * The node object. * @param $count * The number of revisions to be created. */ protected function createRevisions(NodeInterface $node, $count) { for ($i = 0; $i < $count; $i++) { $node->title = $this->randomString(); $node->untranslatable_string_field->value = $this->randomString(); $node->setNewRevision(TRUE); $node->save(); } }
/** * Publishes a node. * * @param NodeInterface $node * @return int */ protected function publishNode($node) { $node->setPublished(TRUE); return $node->save(); }
/** * Tests the tag cache plugin. */ public function testTagCaching() { $view = Views::getView('test_tag_cache'); $view->render(); // Saving the view should invalidate the tags. $cache_plugin = $view->display_handler->getPlugin('cache'); $this->assertTrue($cache_plugin->cacheGet('results'), 'Results cache found.'); $this->assertTrue($cache_plugin->cacheGet('output'), 'Output cache found.'); $view->storage->save(); $this->assertFalse($cache_plugin->cacheGet('results'), 'Results cache empty after the view is saved.'); $this->assertFalse($cache_plugin->cacheGet('output'), 'Output cache empty after the view is saved.'); $view->destroy(); $view->render(); // Test invalidating the nodes in this view invalidates the cache. $cache_plugin = $view->display_handler->getPlugin('cache'); $this->assertTrue($cache_plugin->cacheGet('results'), 'Results cache found.'); $this->assertTrue($cache_plugin->cacheGet('output'), 'Output cache found.'); $this->nodeViewBuilder->resetCache($this->pages); $this->assertFalse($cache_plugin->cacheGet('results'), 'Results cache empty after resetCache is called with pages.'); $this->assertFalse($cache_plugin->cacheGet('output'), 'Output cache empty after resetCache is called with pages.'); $view->destroy(); $view->render(); // Test saving a node in this view invalidates the cache. $cache_plugin = $view->display_handler->getPlugin('cache'); $this->assertTrue($cache_plugin->cacheGet('results'), 'Results cache found.'); $this->assertTrue($cache_plugin->cacheGet('output'), 'Output cache found.'); $node = reset($this->pages); $node->save(); $this->assertFalse($cache_plugin->cacheGet('results'), 'Results cache empty after a page node is saved.'); $this->assertFalse($cache_plugin->cacheGet('output'), 'Output cache empty after a page node is saved.'); $view->destroy(); $view->render(); // Test saving a node not in this view invalidates the cache too. $cache_plugin = $view->display_handler->getPlugin('cache'); $this->assertTrue($cache_plugin->cacheGet('results'), 'Results cache found.'); $this->assertTrue($cache_plugin->cacheGet('output'), 'Output cache found.'); $this->article->save(); $this->assertFalse($cache_plugin->cacheGet('results'), 'Results cache empty after an article node is saved.'); $this->assertFalse($cache_plugin->cacheGet('output'), 'Output cache empty after an article node is saved.'); $view->destroy(); $view->render(); // Test that invalidating a tag for a user, does not invalidate the cache, // as the user entity type will not be contained in the views cache tags. $cache_plugin = $view->display_handler->getPlugin('cache'); $this->assertTrue($cache_plugin->cacheGet('results'), 'Results cache found.'); $this->assertTrue($cache_plugin->cacheGet('output'), 'Output cache found.'); $this->userViewBuilder->resetCache(array($this->user)); $cache_plugin = $view->display_handler->getPlugin('cache'); $this->assertTrue($cache_plugin->cacheGet('results'), 'Results cache found after a user is invalidated.'); $this->assertTrue($cache_plugin->cacheGet('output'), 'Output cache found after a user is invalidated.'); $view->destroy(); $view->render(); // Test the cacheFlush method invalidates the cache. $cache_plugin = $view->display_handler->getPlugin('cache'); $this->assertTrue($cache_plugin->cacheGet('results'), 'Results cache found.'); $this->assertTrue($cache_plugin->cacheGet('output'), 'Output cache found.'); $cache_plugin->cacheFlush(); $cache_plugin = $view->display_handler->getPlugin('cache'); $this->assertFalse($cache_plugin->cacheGet('results'), 'Results cache empty after the cacheFlush() method is called.'); $this->assertFalse($cache_plugin->cacheGet('output'), 'Output cache empty after the cacheFlush() method is called.'); }
/** * Creates a series of revisions for the specified node. * * @param \Drupal\node\NodeInterface $node * The node object. * @param $count * The number of revisions to be created. */ protected function createRevisions(NodeInterface $node, $count) { for ($i = 0; $i < $count; $i++) { $node->title = $this->randomString(); $node->body = [ 'value' => $this->randomMachineName(32), 'format' => filter_default_format(), ]; $node->untranslatable_string_field->value = $this->randomString(); $node->setNewRevision(TRUE); $node->save(); } }