View source
<?php
namespace Drupal\Tests\override_node_options\Functional;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\node\NodeInterface;
use Drupal\Tests\BrowserTestBase;
class NodeFormTest extends BrowserTestBase {
protected $normalUser;
protected $node;
public static $modules = [
'override_node_options',
];
protected $defaultTheme = 'stark';
public function setUp() {
parent::setUp();
$types = NodeType::loadMultiple();
if (empty($types['article'])) {
$this
->drupalCreateContentType([
'type' => 'page',
'name' => t('Page'),
]);
}
$this->normalUser = $this
->drupalCreateUser([
'create page content',
'edit any page content',
]);
$this->node = $this
->drupalCreateNode();
}
public function assertNodeFieldsUpdated(NodeInterface $node, array $fields, $vid = NULL) {
if (!$vid) {
$node = Node::load($node
->id());
}
if ($vid) {
$node = node_revision_load($vid);
}
foreach ($fields as $field => $value) {
self::assertEquals($node
->get($field)->value, $value, t('Node :field was updated to :value, expected :expected.', [
':field' => $field,
':value' => $node
->get($field)->value,
':expected' => $value,
]));
}
}
public function assertNodeFieldsNoAccess(NodeInterface $node, array $fields) {
$this
->drupalGet('node/add/' . $node
->getType());
foreach ($fields as $field) {
$this
->assertSession()
->fieldNotExists($field);
}
$this
->drupalGet('node/' . $this->node
->id() . '/edit');
foreach ($fields as $field) {
$this
->assertSession()
->fieldNotExists($field);
}
}
public function testNodeOptions() {
$specific_user = $this
->drupalCreateUser([
'create page content',
'edit any page content',
'override page published option',
'override page promote to front page option',
'override page sticky option',
]);
$general_user = $this
->drupalCreateUser([
'create page content',
'edit any page content',
'override all published option',
'override all promote to front page option',
'override all sticky option',
]);
$fields = [
'promote' => TRUE,
'status' => TRUE,
'sticky' => TRUE,
];
foreach ([
$specific_user,
$general_user,
] as $user) {
$this
->drupalLogin($user);
$this
->drupalPostForm("node/{$this->node->id()}/edit", [
'promote[value]' => TRUE,
'status[value]' => TRUE,
'sticky[value]' => TRUE,
], t('Save'));
$this
->assertNodeFieldsUpdated($this->node, $fields);
}
$this
->drupalLogin($this->normalUser);
$this
->assertNodeFieldsNoAccess($this->node, array_keys($fields));
}
public function testNodeRevisions() {
$specific_user = $this
->drupalCreateUser([
'create page content',
'edit any page content',
'enter page revision log entry',
'override page revision option',
]);
$general_user = $this
->drupalCreateUser([
'create page content',
'edit any page content',
'enter all revision log entry',
'override all revision option',
]);
$fields = [
'revision' => TRUE,
'revision_log' => TRUE,
];
foreach ([
$specific_user,
$general_user,
] as $user) {
$this
->drupalLogin($user);
$this
->drupalPostForm('node/' . $this->node
->id() . '/edit', [
'revision' => TRUE,
'revision_log[0][value]' => '',
], t('Save'));
$this
->assertNodeFieldsUpdated($this->node, [], $this->node
->getRevisionId());
}
$this
->drupalLogin($this->normalUser);
$this
->assertNodeFieldsNoAccess($this->node, array_keys($fields));
}
public function testNodeAuthor() {
$specific_user = $this
->drupalCreateUser([
'create page content',
'edit any page content',
'override page authored on option',
'override page authored by option',
]);
$general_user = $this
->drupalCreateUser([
'create page content',
'edit any page content',
'override all authored by option',
'override all authored on option',
]);
$time = \Drupal::time()
->getCurrentTime();
$formatter = \Drupal::service('date.formatter');
$fields = [
'created[0][value][date]' => $formatter
->format($time, 'custom', 'Y-m-d'),
'created[0][value][time]' => $formatter
->format($time, 'custom', 'H:i:s'),
'uid[0][target_id]' => '',
];
foreach ([
$specific_user,
$general_user,
] as $user) {
$this
->drupalLogin($user);
$this
->drupalPostForm('node/' . $this->node
->id() . '/edit', [
'uid[0][target_id]' => 'invalid-user',
], t('Save'));
$this
->assertSession()
->pageTextContains('There are no entities matching "invalid-user".');
$this
->drupalPostForm('node/' . $this->node
->id() . '/edit', [
'created[0][value][date]' => 'invalid-date',
], t('Save'));
$this
->assertSession()
->pageTextContains('The Authored on date is invalid.');
$this
->drupalPostForm('node/' . $this->node
->id() . '/edit', $fields, t('Save'));
$this
->assertNodeFieldsUpdated($this->node, [
'created' => $time,
'uid' => 0,
]);
}
$this
->drupalLogin($this->normalUser);
$this
->assertNodeFieldsNoAccess($this->node, array_keys($fields));
}
}