View source
<?php
namespace Drupal\private_content\Tests;
use Drupal\simpletest\WebTestBase;
class PrivateTest extends WebTestBase {
public static $modules = array(
'node',
'search',
'private_content',
);
public function setUp() {
parent::setUp();
$this
->createContentType([
'type' => 'article',
]);
node_access_rebuild();
}
function testNodeAccessBasic() {
$num_simple_users = 3;
$simple_users = array();
$nodes_by_user = array();
$titles = array();
$private_nodes = array();
for ($i = 0; $i < $num_simple_users; $i++) {
$simple_users[$i] = $this
->drupalCreateUser(array(
'access content',
'create article content',
'search content',
'mark content as private',
));
}
foreach ($simple_users as $web_user) {
$this
->drupalLogin($web_user);
foreach (array(
0 => 'Public',
1 => 'Private',
) as $is_private => $type) {
$edit = array(
'title[0][value]' => "{$type} Article created by " . $web_user->name->value,
);
if ($is_private) {
$edit['private'] = TRUE;
$edit['body[0][value]'] = 'private node';
}
else {
$edit['body[0][value]'] = 'public node';
}
$this
->drupalPostForm('node/add/article', $edit, 'Save');
$nid = db_query('SELECT nid FROM {node_field_data} WHERE title = :title', array(
':title' => $edit['title[0][value]'],
))
->fetchField();
debug($nid, 'getting nid');
$node = node_load($nid);
$this
->assertEqual($is_private, $node->private->value, 'Node was properly set to private or not private in private field.');
if ($is_private) {
$private_nodes[] = $nid;
}
$titles[$nid] = $edit['title[0][value]'];
$nodes_by_user[$web_user
->id()][$nid] = $is_private;
}
}
debug($nodes_by_user);
$this
->cronRun();
foreach ($simple_users as $web_user) {
$this
->drupalLogin($web_user);
$this
->checkSearchResults('Private node', 1);
foreach (array_keys($nodes_by_user) as $uid) {
if ($uid == $web_user
->id()) {
foreach ($nodes_by_user[$uid] as $nid => $is_private) {
$this
->drupalGet('node/' . $nid);
$this
->assertResponse(200);
$this
->assertTitle($titles[$nid] . ' | Drupal', t('Correct title for node found'));
}
}
else {
foreach ($nodes_by_user[$uid] as $nid => $is_private) {
$this
->drupalGet('node/' . $nid);
$this
->assertResponse($is_private ? 403 : 200, "Node {$nid} by user {$uid} should get a " . ($is_private ? 403 : 200) . "for this user (" . $web_user
->id() . ")");
if (!$is_private) {
$this
->assertTitle($titles[$nid] . ' | Drupal', t('Correct title for node was found'));
}
}
}
}
}
$access_user = $this
->drupalCreateUser(array(
'access content',
'create article content',
'access private content',
'search content',
));
$this
->drupalLogin($access_user);
$this
->checkSearchResults('Private node', 3);
foreach ($nodes_by_user as $uid => $private_status) {
foreach ($private_status as $nid => $is_private) {
$this
->drupalGet('node/' . $nid);
$this
->assertResponse(200);
}
}
$edit_user = $this
->drupalCreateUser(array(
'access content',
'access private content',
'edit private content',
'edit any article content',
'delete any article content',
));
$this
->drupalLogin($edit_user);
foreach ($private_nodes as $nid) {
$body = $this
->randomString(200);
$edit = array(
'body[0][value]' => $body,
);
$this
->drupalPostForm('node/' . $nid . '/edit', $edit, 'Save');
$this
->assertText('has been updated');
$this
->drupalGet('node/' . $nid . '/delete');
$this
->drupalPostForm(NULL, array(), 'Delete');
$this
->assertText(t('has been deleted'));
}
}
function checkSearchResults($search_query, $expected_result_count) {
$this
->drupalPostForm('search/node', array(
'keys' => $search_query,
), 'Search');
$search_results = $this
->xpath("//ol[contains(@class, 'search-results')]/li");
$this
->assertEqual(count($search_results), $expected_result_count, t('Found the expected number of search results'));
}
}