Skip to content
Snippets Groups Projects
Unverified Commit 72d0bff4 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2865407 by nlisgo, ApacheEx, GoZ, dawehner, michielnugter, Lendude,...

Issue #2865407 by nlisgo, ApacheEx, GoZ, dawehner, michielnugter, Lendude, Mile23: Convert web tests to browser tests for contextual module
parent 3509098e
No related branches found
No related tags found
No related merge requests found
<?php <?php
namespace Drupal\contextual\Tests; namespace Drupal\Tests\contextual\Functional;
use Drupal\Component\Serialization\Json; use Drupal\Component\Serialization\Json;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\language\Entity\ConfigurableLanguage; use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Template\Attribute; use Drupal\Core\Template\Attribute;
use Drupal\Tests\BrowserTestBase;
/** /**
* Tests if contextual links are showing on the front page depending on * Tests if contextual links are showing on the front page depending on
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
* *
* @group contextual * @group contextual
*/ */
class ContextualDynamicContextTest extends WebTestBase { class ContextualDynamicContextTest extends BrowserTestBase {
/** /**
* A user with permission to access contextual links and edit content. * A user with permission to access contextual links and edit content.
...@@ -89,12 +89,12 @@ public function testDifferentPermissions() { ...@@ -89,12 +89,12 @@ public function testDifferentPermissions() {
for ($i = 0; $i < count($ids); $i++) { for ($i = 0; $i < count($ids); $i++) {
$this->assertContextualLinkPlaceHolder($ids[$i]); $this->assertContextualLinkPlaceHolder($ids[$i]);
} }
$this->renderContextualLinks([], 'node'); $response = $this->renderContextualLinks([], 'node');
$this->assertResponse(400); $this->assertSame(400, $response->getStatusCode());
$this->assertRaw('No contextual ids specified.'); $this->assertContains('No contextual ids specified.', (string) $response->getBody());
$response = $this->renderContextualLinks($ids, 'node'); $response = $this->renderContextualLinks($ids, 'node');
$this->assertResponse(200); $this->assertSame(200, $response->getStatusCode());
$json = Json::decode($response); $json = Json::decode((string) $response->getBody());
$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[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[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[2]], '<ul class="contextual-links"><li class="entitynodeedit-form"><a href="' . base_path() . 'node/3/edit">Edit</a></li></ul>');
...@@ -112,12 +112,12 @@ public function testDifferentPermissions() { ...@@ -112,12 +112,12 @@ public function testDifferentPermissions() {
for ($i = 0; $i < count($ids); $i++) { for ($i = 0; $i < count($ids); $i++) {
$this->assertContextualLinkPlaceHolder($ids[$i]); $this->assertContextualLinkPlaceHolder($ids[$i]);
} }
$this->renderContextualLinks([], 'node'); $response = $this->renderContextualLinks([], 'node');
$this->assertResponse(400); $this->assertSame(400, $response->getStatusCode());
$this->assertRaw('No contextual ids specified.'); $this->assertContains('No contextual ids specified.', (string) $response->getBody());
$response = $this->renderContextualLinks($ids, 'node'); $response = $this->renderContextualLinks($ids, 'node');
$this->assertResponse(200); $this->assertSame(200, $response->getStatusCode());
$json = Json::decode($response); $json = Json::decode((string) $response->getBody());
$this->assertIdentical($json[$ids[0]], ''); $this->assertIdentical($json[$ids[0]], '');
$this->assertIdentical($json[$ids[1]], ''); $this->assertIdentical($json[$ids[1]], '');
$this->assertIdentical($json[$ids[2]], ''); $this->assertIdentical($json[$ids[2]], '');
...@@ -129,10 +129,10 @@ public function testDifferentPermissions() { ...@@ -129,10 +129,10 @@ public function testDifferentPermissions() {
for ($i = 0; $i < count($ids); $i++) { for ($i = 0; $i < count($ids); $i++) {
$this->assertNoContextualLinkPlaceHolder($ids[$i]); $this->assertNoContextualLinkPlaceHolder($ids[$i]);
} }
$this->renderContextualLinks([], 'node'); $response = $this->renderContextualLinks([], 'node');
$this->assertResponse(403); $this->assertSame(403, $response->getStatusCode());
$this->renderContextualLinks($ids, 'node'); $this->renderContextualLinks($ids, 'node');
$this->assertResponse(403); $this->assertSame(403, $response->getStatusCode());
// Get a page where contextual links are directly rendered. // Get a page where contextual links are directly rendered.
$this->drupalGet(Url::fromRoute('menu_test.contextual_test')); $this->drupalGet(Url::fromRoute('menu_test.contextual_test'));
...@@ -174,15 +174,23 @@ protected function assertNoContextualLinkPlaceHolder($id) { ...@@ -174,15 +174,23 @@ protected function assertNoContextualLinkPlaceHolder($id) {
* @param string $current_path * @param string $current_path
* The Drupal path for the page for which the contextual links are rendered. * The Drupal path for the page for which the contextual links are rendered.
* *
* @return string * @return \Psr\Http\Message\ResponseInterface
* The response body. * The response object.
*/ */
protected function renderContextualLinks($ids, $current_path) { protected function renderContextualLinks($ids, $current_path) {
$post = []; $http_client = $this->getHttpClient();
for ($i = 0; $i < count($ids); $i++) { $url = Url::fromRoute('contextual.render', [], [
$post['ids[' . $i . ']'] = $ids[$i]; 'query' => [
} '_format' => 'json',
return $this->drupalPostWithFormat('contextual/render', 'json', $post, ['query' => ['destination' => $current_path]]); 'destination' => $current_path,
],
]);
return $http_client->request('POST', $this->buildUrl($url), [
'cookies' => $this->getSessionCookies(),
'form_params' => ['ids' => $ids],
'http_errors' => FALSE,
]);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment