Update to Drupal 8.1.1. For more information, see https://www.drupal.org/node/2718713

This commit is contained in:
Pantheon Automation 2016-05-04 14:35:41 -07:00 committed by Greg Anderson
parent c0a0d5a94c
commit 9eae24d844
669 changed files with 3873 additions and 1553 deletions

View file

@ -19,6 +19,7 @@
* @see \Drupal\node\Tests\NodeAccessBaseTableTest
*/
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
@ -144,7 +145,7 @@ function node_access_test_add_field(NodeTypeInterface $type) {
/**
* Implements hook_node_access().
*/
function node_access_test_node_access(\Drupal\node\NodeInterface $node, $op, \Drupal\Core\Session\AccountInterface $account) {
function node_access_test_node_access(NodeInterface $node, $op, AccountInterface $account) {
$secret_catalan = \Drupal::state()
->get('node_access_test_secret_catalan') ?: 0;
if ($secret_catalan && $node->language()->getId() == 'ca') {

View file

@ -8,6 +8,7 @@
* interaction with the Node module.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\NodeInterface;
@ -151,7 +152,7 @@ function node_test_node_update(NodeInterface $node) {
/**
* Implements hook_entity_view_mode_alter().
*/
function node_test_entity_view_mode_alter(&$view_mode, Drupal\Core\Entity\EntityInterface $entity, $context) {
function node_test_entity_view_mode_alter(&$view_mode, EntityInterface $entity, $context) {
// Only alter the view mode if we are on the test callback.
$change_view_mode = \Drupal::state()->get( 'node_test_change_view_mode') ?: '';
if ($change_view_mode) {

View file

@ -0,0 +1,59 @@
<?php
namespace Drupal\Tests\node\Kernel\Config;
use Drupal\node\Entity\NodeType;
use Drupal\KernelTests\KernelTestBase;
/**
* Change content types during config create method invocation.
*
* @group node
*/
class NodeImportChangeTest extends KernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['node', 'field', 'text', 'system', 'node_test_config', 'user'];
/**
* Set the default field storage backend for fields created during tests.
*/
protected function setUp() {
parent::setUp();
// Set default storage backend.
$this->installConfig(array('field', 'node_test_config'));
}
/**
* Tests importing an updated content type.
*/
public function testImportChange() {
$node_type_id = 'default';
$node_type_config_name = "node.type.$node_type_id";
// Simulate config data to import:
// - a modified version (modified label) of the node type config.
$active = $this->container->get('config.storage');
$sync = $this->container->get('config.storage.sync');
$this->copyConfig($active, $sync);
$node_type = $active->read($node_type_config_name);
$new_label = 'Test update import field';
$node_type['name'] = $new_label;
// Save as files in the sync directory.
$sync->write($node_type_config_name, $node_type);
// Import the content of the sync directory.
$this->configImporter()->import();
// Check that the updated config was correctly imported.
$node_type = NodeType::load($node_type_id);
$this->assertEqual($node_type->label(), $new_label, 'Node type name has been updated.');
}
}

View file

@ -0,0 +1,75 @@
<?php
namespace Drupal\Tests\node\Kernel\Config;
use Drupal\field\Entity\FieldConfig;
use Drupal\node\Entity\NodeType;
use Drupal\KernelTests\KernelTestBase;
/**
* Create content types during config create method invocation.
*
* @group node
*/
class NodeImportCreateTest extends KernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('node', 'field', 'text', 'system', 'user');
/**
* Set the default field storage backend for fields created during tests.
*/
protected function setUp() {
parent::setUp();
$this->installEntitySchema('user');
// Set default storage backend.
$this->installConfig(array('field'));
}
/**
* Tests creating a content type during default config import.
*/
public function testImportCreateDefault() {
$node_type_id = 'default';
// Check that the content type does not exist yet.
$this->assertFalse(NodeType::load($node_type_id));
// Enable node_test_config module and check that the content type
// shipped in the module's default config is created.
$this->container->get('module_installer')->install(array('node_test_config'));
$node_type = NodeType::load($node_type_id);
$this->assertTrue($node_type, 'The default content type was created.');
}
/**
* Tests creating a content type during config import.
*/
public function testImportCreate() {
$node_type_id = 'import';
$node_type_config_name = "node.type.$node_type_id";
// Simulate config data to import.
$active = $this->container->get('config.storage');
$sync = $this->container->get('config.storage.sync');
$this->copyConfig($active, $sync);
// Manually add new node type.
$src_dir = __DIR__ . '/../../../modules/node_test_config/sync';
$target_dir = config_get_config_directory(CONFIG_SYNC_DIRECTORY);
$this->assertTrue(file_unmanaged_copy("$src_dir/$node_type_config_name.yml", "$target_dir/$node_type_config_name.yml"));
// Import the content of the sync directory.
$this->configImporter()->import();
// Check that the content type was created.
$node_type = NodeType::load($node_type_id);
$this->assertTrue($node_type, 'Import node type from sync was created.');
$this->assertFalse(FieldConfig::loadByName('node', $node_type_id, 'body'));
}
}

View file

@ -0,0 +1,54 @@
<?php
namespace Drupal\Tests\node\Kernel;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\node\Entity\NodeType;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests node body field storage.
*
* @group node
*/
class NodeBodyFieldStorageTest extends KernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['user', 'system', 'field', 'node', 'text', 'filter'];
protected function setUp() {
parent::setUp();
$this->installSchema('system', 'sequences');
// Necessary for module uninstall.
$this->installSchema('user', 'users_data');
$this->installEntitySchema('user');
$this->installEntitySchema('node');
$this->installConfig(array('field', 'node'));
}
/**
* Tests node body field storage persistence even if there are no instances.
*/
public function testFieldOverrides() {
$field_storage = FieldStorageConfig::loadByName('node', 'body');
$this->assertTrue($field_storage, 'Node body field storage exists.');
$type = NodeType::create(['name' => 'Ponies', 'type' => 'ponies']);
$type->save();
node_add_body_field($type);
$field_storage = FieldStorageConfig::loadByName('node', 'body');
$this->assertTrue(count($field_storage->getBundles()) == 1, 'Node body field storage is being used on the new node type.');
$field = FieldConfig::loadByName('node', 'ponies', 'body');
$field->delete();
$field_storage = FieldStorageConfig::loadByName('node', 'body');
$this->assertTrue(count($field_storage->getBundles()) == 0, 'Node body field storage exists after deleting the only instance of a field.');
\Drupal::service('module_installer')->uninstall(array('node'));
$field_storage = FieldStorageConfig::loadByName('node', 'body');
$this->assertFalse($field_storage, 'Node body field storage does not exist after uninstalling the Node module.');
}
}

View file

@ -0,0 +1,82 @@
<?php
namespace Drupal\Tests\node\Kernel;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
/**
* Tests that conditions, provided by the node module, are working properly.
*
* @group node
*/
class NodeConditionTest extends EntityKernelTestBase {
public static $modules = array('node');
protected function setUp() {
parent::setUp();
// Create the node bundles required for testing.
$type = NodeType::create(['type' => 'page', 'name' => 'page']);
$type->save();
$type = NodeType::create(['type' => 'article', 'name' => 'article']);
$type->save();
$type = NodeType::create(['type' => 'test', 'name' => 'test']);
$type->save();
}
/**
* Tests conditions.
*/
function testConditions() {
$manager = $this->container->get('plugin.manager.condition', $this->container->get('container.namespaces'));
$this->createUser();
// Get some nodes of various types to check against.
$page = Node::create(['type' => 'page', 'title' => $this->randomMachineName(), 'uid' => 1]);
$page->save();
$article = Node::create(['type' => 'article', 'title' => $this->randomMachineName(), 'uid' => 1]);
$article->save();
$test = Node::create(['type' => 'test', 'title' => $this->randomMachineName(), 'uid' => 1]);
$test->save();
// Grab the node type condition and configure it to check against node type
// of 'article' and set the context to the page type node.
$condition = $manager->createInstance('node_type')
->setConfig('bundles', array('article' => 'article'))
->setContextValue('node', $page);
$this->assertFalse($condition->execute(), 'Page type nodes fail node type checks for articles.');
// Check for the proper summary.
$this->assertEqual('The node bundle is article', $condition->summary());
// Set the node type check to page.
$condition->setConfig('bundles', array('page' => 'page'));
$this->assertTrue($condition->execute(), 'Page type nodes pass node type checks for pages');
// Check for the proper summary.
$this->assertEqual('The node bundle is page', $condition->summary());
// Set the node type check to page or article.
$condition->setConfig('bundles', array('page' => 'page', 'article' => 'article'));
$this->assertTrue($condition->execute(), 'Page type nodes pass node type checks for pages or articles');
// Check for the proper summary.
$this->assertEqual('The node bundle is page or article', $condition->summary());
// Set the context to the article node.
$condition->setContextValue('node', $article);
$this->assertTrue($condition->execute(), 'Article type nodes pass node type checks for pages or articles');
// Set the context to the test node.
$condition->setContextValue('node', $test);
$this->assertFalse($condition->execute(), 'Test type nodes pass node type checks for pages or articles');
// Check a greater than 2 bundles summary scenario.
$condition->setConfig('bundles', array('page' => 'page', 'article' => 'article', 'test' => 'test'));
$this->assertEqual('The node bundle is page, article or test', $condition->summary());
// Test Constructor injection.
$condition = $manager->createInstance('node_type', array('bundles' => array('article' => 'article'), 'context' => array('node' => $article)));
$this->assertTrue($condition->execute(), 'Constructor injection of context and configuration working as anticipated.');
}
}

View file

@ -0,0 +1,149 @@
<?php
namespace Drupal\Tests\node\Kernel;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
/**
* Tests node field level access.
*
* @group node
*/
class NodeFieldAccessTest extends EntityKernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('node');
/**
* Fields that only users with administer nodes permissions can change.
*
* @var array
*/
protected $administrativeFields = array(
'status',
'promote',
'sticky',
'created',
'uid',
);
/**
* These fields are automatically managed and can not be changed by any user.
*
* @var array
*/
protected $readOnlyFields = array('changed', 'revision_uid', 'revision_timestamp');
/**
* Test permissions on nodes status field.
*/
function testAccessToAdministrativeFields() {
// Create the page node type with revisions disabled.
$page = NodeType::create([
'type' => 'page',
'new_revision' => FALSE,
]);
$page->save();
// Create the article node type with revisions disabled.
$article = NodeType::create([
'type' => 'article',
'new_revision' => TRUE,
]);
$article->save();
// An administrator user. No user exists yet, ensure that the first user
// does not have UID 1.
$content_admin_user = $this->createUser(array('uid' => 2), array('administer nodes'));
// Two different editor users.
$page_creator_user = $this->createUser(array(), array('create page content', 'edit own page content', 'delete own page content'));
$page_manager_user = $this->createUser(array(), array('create page content', 'edit any page content', 'delete any page content'));
// An unprivileged user.
$page_unrelated_user = $this->createUser(array(), array('access content'));
// List of all users
$test_users = array(
$content_admin_user,
$page_creator_user,
$page_manager_user,
$page_unrelated_user,
);
// Create three "Basic pages". One is owned by our test-user
// "page_creator", one by "page_manager", and one by someone else.
$node1 = Node::create(array(
'title' => $this->randomMachineName(8),
'uid' => $page_creator_user->id(),
'type' => 'page',
));
$node2 = Node::create(array(
'title' => $this->randomMachineName(8),
'uid' => $page_manager_user->id(),
'type' => 'article',
));
$node3 = Node::create(array(
'title' => $this->randomMachineName(8),
'type' => 'page',
));
foreach ($this->administrativeFields as $field) {
// Checks on view operations.
foreach ($test_users as $account) {
$may_view = $node1->{$field}->access('view', $account);
$this->assertTrue($may_view, SafeMarkup::format('Any user may view the field @name.', array('@name' => $field)));
}
// Checks on edit operations.
$may_update = $node1->{$field}->access('edit', $page_creator_user);
$this->assertFalse($may_update, SafeMarkup::format('Users with permission "edit own page content" is not allowed to the field @name.', array('@name' => $field)));
$may_update = $node2->{$field}->access('edit', $page_creator_user);
$this->assertFalse($may_update, SafeMarkup::format('Users with permission "edit own page content" is not allowed to the field @name.', array('@name' => $field)));
$may_update = $node2->{$field}->access('edit', $page_manager_user);
$this->assertFalse($may_update, SafeMarkup::format('Users with permission "edit any page content" is not allowed to the field @name.', array('@name' => $field)));
$may_update = $node1->{$field}->access('edit', $page_manager_user);
$this->assertFalse($may_update, SafeMarkup::format('Users with permission "edit any page content" is not allowed to the field @name.', array('@name' => $field)));
$may_update = $node2->{$field}->access('edit', $page_unrelated_user);
$this->assertFalse($may_update, SafeMarkup::format('Users not having permission "edit any page content" is not allowed to the field @name.', array('@name' => $field)));
$may_update = $node1->{$field}->access('edit', $content_admin_user) && $node3->status->access('edit', $content_admin_user);
$this->assertTrue($may_update, SafeMarkup::format('Users with permission "administer nodes" may edit @name fields on all nodes.', array('@name' => $field)));
}
foreach ($this->readOnlyFields as $field) {
// Check view operation.
foreach ($test_users as $account) {
$may_view = $node1->{$field}->access('view', $account);
$this->assertTrue($may_view, SafeMarkup::format('Any user may view the field @name.', array('@name' => $field)));
}
// Check edit operation.
foreach ($test_users as $account) {
$may_view = $node1->{$field}->access('edit', $account);
$this->assertFalse($may_view, SafeMarkup::format('No user is not allowed to edit the field @name.', array('@name' => $field)));
}
}
// Check the revision_log field on node 1 which has revisions disabled.
$may_update = $node1->revision_log->access('edit', $content_admin_user);
$this->assertTrue($may_update, 'A user with permission "administer nodes" can edit the revision_log field when revisions are disabled.');
$may_update = $node1->revision_log->access('edit', $page_creator_user);
$this->assertFalse($may_update, 'A user without permission "administer nodes" can not edit the revision_log field when revisions are disabled.');
// Check the revision_log field on node 2 which has revisions enabled.
$may_update = $node2->revision_log->access('edit', $content_admin_user);
$this->assertTrue($may_update, 'A user with permission "administer nodes" can edit the revision_log field when revisions are enabled.');
$may_update = $node2->revision_log->access('edit', $page_creator_user);
$this->assertTrue($may_update, 'A user without permission "administer nodes" can edit the revision_log field when revisions are enabled.');
}
}

View file

@ -0,0 +1,64 @@
<?php
namespace Drupal\Tests\node\Kernel;
use Drupal\user\UserInterface;
use Drupal\Core\Field\Entity\BaseFieldOverride;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
/**
* Tests node field overrides.
*
* @group node
*/
class NodeFieldOverridesTest extends EntityKernelTestBase {
/**
* Current logged in user.
*
* @var \Drupal\user\UserInterface
*/
protected $user;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('user', 'system', 'field', 'node');
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installConfig(array('user'));
$this->user = $this->createUser();
\Drupal::service('current_user')->setAccount($this->user);
}
/**
* Tests that field overrides work as expected.
*/
public function testFieldOverrides() {
if (!NodeType::load('ponies')) {
NodeType::create(['name' => 'Ponies', 'type' => 'ponies'])->save();
}
$override = BaseFieldOverride::loadByName('node', 'ponies', 'uid');
if ($override) {
$override->delete();
}
$uid_field = \Drupal::entityManager()->getBaseFieldDefinitions('node')['uid'];
$config = $uid_field->getConfig('ponies');
$config->save();
$this->assertEqual($config->get('default_value_callback'), 'Drupal\node\Entity\Node::getCurrentUserId');
/** @var \Drupal\node\NodeInterface $node */
$node = Node::create(['type' => 'ponies']);
$owner = $node->getOwner();
$this->assertTrue($owner instanceof UserInterface);
$this->assertEqual($owner->id(), $this->user->id());
}
}

View file

@ -0,0 +1,40 @@
<?php
namespace Drupal\Tests\node\Kernel;
use Drupal\Core\Language\LanguageInterface;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests the admin listing fallback when views is not enabled.
*
* @group node
*/
class NodeListBuilderTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['node', 'user'];
protected function setUp() {
parent::setUp();
$this->installEntitySchema('node');
}
/**
* Tests that the correct cache contexts are set.
*/
public function testCacheContexts() {
/** @var \Drupal\Core\Entity\EntityListBuilderInterface $list_builder */
$list_builder = $this->container->get('entity.manager')->getListBuilder('node');
$build = $list_builder->render();
$this->container->get('renderer')->renderRoot($build);
$this->assertEqual(['languages:' . LanguageInterface::TYPE_INTERFACE, 'theme', 'url.query_args.pagers:0', 'user.node_grants:view', 'user.permissions'], $build['#cache']['contexts']);
}
}

View file

@ -0,0 +1,78 @@
<?php
namespace Drupal\Tests\node\Kernel;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
/**
* Tests node owner functionality.
*
* @group Entity
*/
class NodeOwnerTest extends EntityKernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('node', 'language');
protected function setUp() {
parent::setUp();
// Create the node bundles required for testing.
$type = NodeType::create(array(
'type' => 'page',
'name' => 'page',
));
$type->save();
// Enable two additional languages.
ConfigurableLanguage::createFromLangcode('de')->save();
ConfigurableLanguage::createFromLangcode('it')->save();
$this->installSchema('node', 'node_access');
}
/**
* Tests node owner functionality.
*/
public function testOwner() {
$user = $this->createUser();
$container = \Drupal::getContainer();
$container->get('current_user')->setAccount($user);
// Create a test node.
$english = Node::create(array(
'type' => 'page',
'title' => $this->randomMachineName(),
'language' => 'en',
));
$english->save();
$this->assertEqual($user->id(), $english->getOwnerId());
$german = $english->addTranslation('de');
$german->title = $this->randomString();
$italian = $english->addTranslation('it');
$italian->title = $this->randomString();
// Node::preSave() sets owner to anonymous user if owner is nor set.
$english->set('uid', ['target_id' => NULL]);
$german->set('uid', ['target_id' => NULL]);
$italian->set('uid', ['target_id' => NULL]);
// Entity::save() saves all translations!
$italian->save();
$this->assertEqual(0, $english->getOwnerId());
$this->assertEqual(0, $german->getOwnerId());
$this->assertEqual(0, $italian->getOwnerId());
}
}

View file

@ -0,0 +1,131 @@
<?php
namespace Drupal\Tests\node\Kernel;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Html;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\system\Kernel\Token\TokenReplaceKernelTestBase;
/**
* Generates text using placeholders for dummy content to check node token
* replacement.
*
* @group node
*/
class NodeTokenReplaceTest extends TokenReplaceKernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('node', 'filter');
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installConfig(array('filter', 'node'));
$node_type = NodeType::create(['type' => 'article', 'name' => 'Article']);
$node_type->save();
node_add_body_field($node_type);
}
/**
* Creates a node, then tests the tokens generated from it.
*/
function testNodeTokenReplacement() {
$url_options = array(
'absolute' => TRUE,
'language' => $this->interfaceLanguage,
);
// Create a user and a node.
$account = $this->createUser();
/* @var $node \Drupal\node\NodeInterface */
$node = Node::create([
'type' => 'article',
'tnid' => 0,
'uid' => $account->id(),
'title' => '<blink>Blinking Text</blink>',
'body' => [['value' => 'Regular NODE body for the test.', 'summary' => 'Fancy NODE summary.', 'format' => 'plain_text']],
]);
$node->save();
// Generate and test tokens.
$tests = array();
$tests['[node:nid]'] = $node->id();
$tests['[node:vid]'] = $node->getRevisionId();
$tests['[node:type]'] = 'article';
$tests['[node:type-name]'] = 'Article';
$tests['[node:title]'] = Html::escape($node->getTitle());
$tests['[node:body]'] = $node->body->processed;
$tests['[node:summary]'] = $node->body->summary_processed;
$tests['[node:langcode]'] = $node->language()->getId();
$tests['[node:url]'] = $node->url('canonical', $url_options);
$tests['[node:edit-url]'] = $node->url('edit-form', $url_options);
$tests['[node:author]'] = $account->getUsername();
$tests['[node:author:uid]'] = $node->getOwnerId();
$tests['[node:author:name]'] = $account->getUsername();
$tests['[node:created:since]'] = \Drupal::service('date.formatter')->formatTimeDiffSince($node->getCreatedTime(), array('langcode' => $this->interfaceLanguage->getId()));
$tests['[node:changed:since]'] = \Drupal::service('date.formatter')->formatTimeDiffSince($node->getChangedTime(), array('langcode' => $this->interfaceLanguage->getId()));
$base_bubbleable_metadata = BubbleableMetadata::createFromObject($node);
$metadata_tests = [];
$metadata_tests['[node:nid]'] = $base_bubbleable_metadata;
$metadata_tests['[node:vid]'] = $base_bubbleable_metadata;
$metadata_tests['[node:type]'] = $base_bubbleable_metadata;
$metadata_tests['[node:type-name]'] = $base_bubbleable_metadata;
$metadata_tests['[node:title]'] = $base_bubbleable_metadata;
$metadata_tests['[node:body]'] = $base_bubbleable_metadata;
$metadata_tests['[node:summary]'] = $base_bubbleable_metadata;
$metadata_tests['[node:langcode]'] = $base_bubbleable_metadata;
$metadata_tests['[node:url]'] = $base_bubbleable_metadata;
$metadata_tests['[node:edit-url]'] = $base_bubbleable_metadata;
$bubbleable_metadata = clone $base_bubbleable_metadata;
$metadata_tests['[node:author]'] = $bubbleable_metadata->addCacheTags(['user:1']);
$metadata_tests['[node:author:uid]'] = $bubbleable_metadata;
$metadata_tests['[node:author:name]'] = $bubbleable_metadata;
$bubbleable_metadata = clone $base_bubbleable_metadata;
$metadata_tests['[node:created:since]'] = $bubbleable_metadata->setCacheMaxAge(0);
$metadata_tests['[node:changed:since]'] = $bubbleable_metadata;
// Test to make sure that we generated something for each token.
$this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
foreach ($tests as $input => $expected) {
$bubbleable_metadata = new BubbleableMetadata();
$output = $this->tokenService->replace($input, array('node' => $node), array('langcode' => $this->interfaceLanguage->getId()), $bubbleable_metadata);
$this->assertEqual($output, $expected, format_string('Node token %token replaced.', ['%token' => $input]));
$this->assertEqual($bubbleable_metadata, $metadata_tests[$input]);
}
// Repeat for a node without a summary.
$node = Node::create([
'type' => 'article',
'uid' => $account->id(),
'title' => '<blink>Blinking Text</blink>',
'body' => [['value' => 'A string that looks random like TR5c2I', 'format' => 'plain_text']],
]);
$node->save();
// Generate and test token - use full body as expected value.
$tests = array();
$tests['[node:summary]'] = $node->body->processed;
// Test to make sure that we generated something for each token.
$this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated for node without a summary.');
foreach ($tests as $input => $expected) {
$output = $this->tokenService->replace($input, array('node' => $node), array('language' => $this->interfaceLanguage));
$this->assertEqual($output, $expected, new FormattableMarkup('Node token %token replaced for node without a summary.', ['%token' => $input]));
}
}
}

View file

@ -0,0 +1,71 @@
<?php
namespace Drupal\Tests\node\Kernel;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
/**
* Tests node validation constraints.
*
* @group node
*/
class NodeValidationTest extends EntityKernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('node');
/**
* Set the default field storage backend for fields created during tests.
*/
protected function setUp() {
parent::setUp();
// Create a node type for testing.
$type = NodeType::create(['type' => 'page', 'name' => 'page']);
$type->save();
}
/**
* Tests the node validation constraints.
*/
public function testValidation() {
$this->createUser();
$node = Node::create(['type' => 'page', 'title' => 'test', 'uid' => 1]);
$violations = $node->validate();
$this->assertEqual(count($violations), 0, 'No violations when validating a default node.');
$node->set('title', $this->randomString(256));
$violations = $node->validate();
$this->assertEqual(count($violations), 1, 'Violation found when title is too long.');
$this->assertEqual($violations[0]->getPropertyPath(), 'title.0.value');
$this->assertEqual($violations[0]->getMessage(), '<em class="placeholder">Title</em>: may not be longer than 255 characters.');
$node->set('title', NULL);
$violations = $node->validate();
$this->assertEqual(count($violations), 1, 'Violation found when title is not set.');
$this->assertEqual($violations[0]->getPropertyPath(), 'title');
$this->assertEqual($violations[0]->getMessage(), 'This value should not be null.');
$node->set('title', '');
$violations = $node->validate();
$this->assertEqual(count($violations), 1, 'Violation found when title is set to an empty string.');
$this->assertEqual($violations[0]->getPropertyPath(), 'title');
// Make the title valid again.
$node->set('title', $this->randomString());
// Save the node so that it gets an ID and a changed date.
$node->save();
// Set the changed date to something in the far past.
$node->set('changed', 433918800);
$violations = $node->validate();
$this->assertEqual(count($violations), 1, 'Violation found when changed date is before the last changed date.');
$this->assertEqual($violations[0]->getPropertyPath(), '');
$this->assertEqual($violations[0]->getMessage(), 'The content has either been modified by another user, or you have already submitted modifications. As a result, your changes cannot be saved.');
}
}