View source
<?php
namespace Drupal\contextual\Tests;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Url;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Template\Attribute;
class ContextualDynamicContextTest extends WebTestBase {
protected $editorUser;
protected $authenticatedUser;
protected $anonymousUser;
public static $modules = array(
'contextual',
'node',
'views',
'views_ui',
'language',
'menu_test',
);
protected function setUp() {
parent::setUp();
$this
->drupalCreateContentType(array(
'type' => 'page',
'name' => 'Basic page',
));
$this
->drupalCreateContentType(array(
'type' => 'article',
'name' => 'Article',
));
ConfigurableLanguage::createFromLangcode('it')
->save();
$this
->rebuildContainer();
$this->editorUser = $this
->drupalCreateUser(array(
'access content',
'access contextual links',
'edit any article content',
));
$this->authenticatedUser = $this
->drupalCreateUser(array(
'access content',
'access contextual links',
));
$this->anonymousUser = $this
->drupalCreateUser(array(
'access content',
));
}
function testDifferentPermissions() {
$this
->drupalLogin($this->editorUser);
$node1 = $this
->drupalCreateNode(array(
'type' => 'article',
'promote' => 1,
));
$node2 = $this
->drupalCreateNode(array(
'type' => 'page',
'promote' => 1,
));
$node3 = $this
->drupalCreateNode(array(
'type' => 'article',
'promote' => 1,
));
$ids = [
'node:node=' . $node1
->id() . ':changed=' . $node1
->getChangedTime() . '&langcode=en',
'node:node=' . $node2
->id() . ':changed=' . $node2
->getChangedTime() . '&langcode=en',
'node:node=' . $node3
->id() . ':changed=' . $node3
->getChangedTime() . '&langcode=en',
'entity.view.edit_form:view=frontpage:location=page&name=frontpage&display_id=page_1&langcode=en',
];
$this
->drupalGet('node');
for ($i = 0; $i < count($ids); $i++) {
$this
->assertContextualLinkPlaceHolder($ids[$i]);
}
$this
->renderContextualLinks(array(), 'node');
$this
->assertResponse(400);
$this
->assertRaw('No contextual ids specified.');
$response = $this
->renderContextualLinks($ids, 'node');
$this
->assertResponse(200);
$json = Json::decode($response);
$this
->assertIdentical($json[$ids[0]], '<ul class="contextual-links"><li class="entitynodeedit-form"><a href="' . base_path() . 'node/1/edit">Edit</a></li></ul>');
$this
->assertIdentical($json[$ids[1]], '');
$this
->assertIdentical($json[$ids[2]], '<ul class="contextual-links"><li class="entitynodeedit-form"><a href="' . base_path() . 'node/3/edit">Edit</a></li></ul>');
$this
->assertIdentical($json[$ids[3]], '');
$this
->drupalLogin($this->authenticatedUser);
$this
->drupalGet('node');
for ($i = 0; $i < count($ids); $i++) {
$this
->assertContextualLinkPlaceHolder($ids[$i]);
}
$this
->renderContextualLinks(array(), 'node');
$this
->assertResponse(400);
$this
->assertRaw('No contextual ids specified.');
$response = $this
->renderContextualLinks($ids, 'node');
$this
->assertResponse(200);
$json = Json::decode($response);
$this
->assertIdentical($json[$ids[0]], '');
$this
->assertIdentical($json[$ids[1]], '');
$this
->assertIdentical($json[$ids[2]], '');
$this
->assertIdentical($json[$ids[3]], '');
$this
->drupalLogin($this->anonymousUser);
$this
->drupalGet('node');
for ($i = 0; $i < count($ids); $i++) {
$this
->assertContextualLinkPlaceHolder($ids[$i]);
}
$this
->renderContextualLinks(array(), 'node');
$this
->assertResponse(403);
$this
->renderContextualLinks($ids, 'node');
$this
->assertResponse(403);
$node3
->addTranslation('it')
->set('title', $this
->randomString())
->save();
$id = 'node:node=' . $node3
->id() . ':changed=' . $node3
->getChangedTime() . '&langcode=it';
$this
->drupalGet('node', [
'language' => ConfigurableLanguage::createFromLangcode('it'),
]);
$this
->assertContextualLinkPlaceHolder($id);
$this
->drupalGet(Url::fromRoute('menu_test.contextual_test'));
$this
->assertEscaped("<script>alert('Welcome to the jungle!')</script>");
$this
->assertLink('Edit menu - contextual');
}
protected function assertContextualLinkPlaceHolder($id) {
return $this
->assertRaw('<div' . new Attribute(array(
'data-contextual-id' => $id,
)) . '></div>', format_string('Contextual link placeholder with id @id exists.', array(
'@id' => $id,
)));
}
protected function assertNoContextualLinkPlaceHolder($id) {
return $this
->assertNoRaw('<div' . new Attribute(array(
'data-contextual-id' => $id,
)) . '></div>', format_string('Contextual link placeholder with id @id does not exist.', array(
'@id' => $id,
)));
}
protected function renderContextualLinks($ids, $current_path) {
$post = array();
for ($i = 0; $i < count($ids); $i++) {
$post['ids[' . $i . ']'] = $ids[$i];
}
return $this
->drupalPostWithFormat('contextual/render', 'json', $post, array(
'query' => array(
'destination' => $current_path,
),
));
}
}