Unverified Commit bc6b09fc authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2996030 by Lendude, Mile23: Convert web tests to browser tests for node module - round 2

(cherry picked from commit 15ca693b)
parent 1564d992
Loading
Loading
Loading
Loading
+1 −21
Changes for core/modules/node/tests/src/Functional/NodeRevisionsTest.php: 1 added line, 21 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\node\Tests;
namespace Drupal\Tests\node\Functional;

use Drupal\Core\Url;
use Drupal\field\Entity\FieldConfig;
@@ -158,17 +158,6 @@ public function testRevisions() {
    // Confirm that this is the default revision.
    $this->assertTrue($node->isDefaultRevision(), 'Third node revision is the default one.');

    // Confirm that the "Edit" and "Delete" contextual links appear for the
    // default revision.
    $ids = ['node:node=' . $node->id() . ':changed=' . $node->getChangedTime()];
    $json = $this->renderContextualLinks($ids, 'node/' . $node->id());
    $this->verbose($json[$ids[0]]);

    $expected = '<li class="entitynodeedit-form"><a href="' . base_path() . 'node/' . $node->id() . '/edit">Edit</a></li>';
    $this->assertTrue(strstr($json[$ids[0]], $expected), 'The "Edit" contextual link is shown for the default revision.');
    $expected = '<li class="entitynodedelete-form"><a href="' . base_path() . 'node/' . $node->id() . '/delete">Delete</a></li>';
    $this->assertTrue(strstr($json[$ids[0]], $expected), 'The "Delete" contextual link is shown for the default revision.');

    // Confirm that revisions revert properly.
    $this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[1]->getRevisionid() . "/revert", [], t('Revert'));
    $this->assertRaw(t('@type %title has been reverted to the revision from %revision-date.', [
@@ -188,15 +177,6 @@ public function testRevisions() {
    $node = node_revision_load($node->getRevisionId());
    $this->assertFalse($node->isDefaultRevision(), 'Third node revision is not the default one.');

    // Confirm that "Edit" and "Delete" contextual links don't appear for
    // non-default revision.
    $ids = ['node_revision::node=' . $node->id() . '&node_revision=' . $node->getRevisionId() . ':'];
    $json = $this->renderContextualLinks($ids, 'node/' . $node->id() . '/revisions/' . $node->getRevisionId() . '/view');
    $this->verbose($json[$ids[0]]);

    $this->assertFalse(strstr($json[$ids[0]], '<li class="entitynodeedit-form">'), 'The "Edit" contextual link is not shown for a non-default revision.');
    $this->assertFalse(strstr($json[$ids[0]], '<li class="entitynodedelete-form">'), 'The "Delete" contextual link is not shown for a non-default revision.');

    // Confirm revisions delete properly.
    $this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[1]->getRevisionId() . "/delete", [], t('Delete'));
    $this->assertRaw(t('Revision from %revision-date of @type %title has been deleted.', [
+14 −11
Changes for core/modules/node/tests/src/Functional/NodeTypeTest.php: 14 added lines, 11 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\node\Tests;
namespace Drupal\Tests\node\Functional;

use Drupal\field\Entity\FieldConfig;
use Drupal\node\Entity\NodeType;
use Drupal\Core\Url;
use Drupal\system\Tests\Menu\AssertBreadcrumbTrait;
use Drupal\Tests\system\Functional\Menu\AssertBreadcrumbTrait;
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;

/**
 * Ensures that node type functions work correctly.
@@ -15,6 +16,7 @@
class NodeTypeTest extends NodeTestBase {

  use AssertBreadcrumbTrait;
  use AssertPageCacheContextsAndTagsTrait;

  /**
   * Modules to enable.
@@ -87,6 +89,7 @@ public function testNodeTypeCreation() {
   * Tests editing a node type using the UI.
   */
  public function testNodeTypeEditing() {
    $assert = $this->assertSession();
    $this->drupalPlaceBlock('system_breadcrumb_block');
    $web_user = $this->drupalCreateUser(['bypass node access', 'administer content types', 'administer node fields']);
    $this->drupalLogin($web_user);
@@ -96,8 +99,8 @@ public function testNodeTypeEditing() {

    // Verify that title and body fields are displayed.
    $this->drupalGet('node/add/page');
    $this->assertRaw('Title', 'Title field was found.');
    $this->assertRaw('Body', 'Body field was found.');
    $assert->pageTextContains('Title');
    $assert->pageTextContains('Body');

    // Rename the title field.
    $edit = [
@@ -106,8 +109,8 @@ public function testNodeTypeEditing() {
    $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type'));

    $this->drupalGet('node/add/page');
    $this->assertRaw('Foo', 'New title label was displayed.');
    $this->assertNoRaw('Title', 'Old title label was not displayed.');
    $assert->pageTextContains('Foo');
    $assert->pageTextNotContains('Title');

    // Change the name and the description.
    $edit = [
@@ -117,11 +120,11 @@ public function testNodeTypeEditing() {
    $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type'));

    $this->drupalGet('node/add');
    $this->assertRaw('Bar', 'New name was displayed.');
    $this->assertRaw('Lorem ipsum', 'New description was displayed.');
    $assert->pageTextContains('Bar');
    $assert->pageTextContains('Lorem ipsum');
    $this->clickLink('Bar');
    $this->assertRaw('Foo', 'Title field was found.');
    $this->assertRaw('Body', 'Body field was found.');
    $assert->pageTextContains('Foo');
    $assert->pageTextContains('Body');

    // Change the name through the API
    /** @var \Drupal\node\NodeTypeInterface $node_type */
@@ -146,7 +149,7 @@ public function testNodeTypeEditing() {
    ]);
    // Check that the body field doesn't exist.
    $this->drupalGet('node/add/page');
    $this->assertNoRaw('Body', 'Body field was not found.');
    $assert->pageTextNotContains('Body');
  }

  /**
+11 −6
Changes for core/modules/node/tests/src/Functional/PagePreviewTest.php: 11 added lines, 6 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\node\Tests;
namespace Drupal\Tests\node\Functional;

use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
@@ -12,6 +12,7 @@
use Drupal\node\Entity\NodeType;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\Tests\TestFileCreationTrait;

/**
 * Tests the node entity preview functionality.
@@ -22,6 +23,9 @@ class PagePreviewTest extends NodeTestBase {

  use EntityReferenceTestTrait;
  use CommentTestTrait;
  use TestFileCreationTrait {
    getTestFiles as drupalGetTestFiles;
  }

  /**
   * Enable the comment, node and taxonomy modules to test on the preview.
@@ -177,7 +181,8 @@ public function testPagePreview() {
    $this->drupalPostForm(NULL, ['field_image[0][alt]' => 'Picture of llamas'], t('Preview'));

    // Check that the preview is displaying the title, body and term.
    $this->assertTitle(t('@title | Drupal', ['@title' => $edit[$title_key]]), 'Basic page title is preview.');
    $expected_title = $edit[$title_key] . ' | Drupal';
    $this->assertSession()->titleEquals($expected_title);
    $this->assertEscaped($edit[$title_key], 'Title displayed and escaped.');
    $this->assertText($edit[$body_key], 'Body displayed.');
    $this->assertText($edit[$term_key], 'Term displayed.');
@@ -210,13 +215,13 @@ public function testPagePreview() {
    $this->assertFieldByName($body_key, $edit[$body_key], 'Body field displayed.');
    $this->assertFieldByName($term_key, $edit[$term_key], 'Term field displayed.');
    $this->assertFieldByName('field_image[0][alt]', 'Picture of llamas');
    $this->drupalPostAjaxForm(NULL, [], ['field_test_multi_add_more' => t('Add another item')], NULL, [], [], 'node-page-form');
    $this->getSession()->getPage()->pressButton('Add another item');
    $this->assertFieldByName('field_test_multi[0][value]');
    $this->assertFieldByName('field_test_multi[1][value]');

    // Return to page preview to check everything is as expected.
    $this->drupalPostForm(NULL, [], t('Preview'));
    $this->assertTitle(t('@title | Drupal', ['@title' => $edit[$title_key]]), 'Basic page title is preview.');
    $this->assertSession()->titleEquals($expected_title);
    $this->assertEscaped($edit[$title_key], 'Title displayed and escaped.');
    $this->assertText($edit[$body_key], 'Body displayed.');
    $this->assertText($edit[$term_key], 'Term displayed.');
@@ -353,8 +358,8 @@ public function testPagePreview() {
    $this->assertText('Basic page ' . $title . ' has been created.');
    $node = $this->drupalGetNodeByTitle($title);
    $this->drupalGet('node/' . $node->id() . '/edit');
    $this->drupalPostAjaxForm(NULL, [], ['field_test_multi_add_more' => t('Add another item')]);
    $this->drupalPostAjaxForm(NULL, [], ['field_test_multi_add_more' => t('Add another item')]);
    $this->getSession()->getPage()->pressButton('Add another item');
    $this->getSession()->getPage()->pressButton('Add another item');
    $edit = [
      'field_test_multi[1][value]' => $example_text_2,
      'field_test_multi[2][value]' => $example_text_3,
+2 −73
Changes for core/modules/node/tests/src/Functional/Views/NodeContextualLinksTest.php: 2 added lines, 73 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\node\Tests\Views;
namespace Drupal\Tests\node\Functional\Views;

use Drupal\Component\Serialization\Json;
use Drupal\user\Entity\User;

/**
@@ -19,76 +18,6 @@ class NodeContextualLinksTest extends NodeTestBase {
   */
  public static $modules = ['contextual'];

  /**
   * Views used by this test.
   *
   * @var array
   */
  public static $testViews = ['test_contextual_links'];

  /**
   * Tests contextual links.
   */
  public function testNodeContextualLinks() {
    $this->drupalCreateContentType(['type' => 'page']);
    $this->drupalCreateNode(['promote' => 1]);
    $this->drupalGet('node');

    $user = $this->drupalCreateUser(['administer nodes', 'access contextual links']);
    $this->drupalLogin($user);

    $response = $this->renderContextualLinks(['node:node=1:'], 'node');
    $this->assertResponse(200);
    $json = Json::decode($response);
    $this->setRawContent($json['node:node=1:']);

    // @todo Add these back when the functionality for making Views displays
    //   appear in contextual links is working again.
    // $this->assertLinkByHref('node/1/contextual-links', 0, 'The contextual link to the view was found.');
    // $this->assertLink('Test contextual link', 0, 'The contextual link to the view was found.');
  }

  /**
   * Get server-rendered contextual links for the given contextual link ids.
   *
   * Copied from \Drupal\contextual\Tests\ContextualDynamicContextTest::renderContextualLinks().
   *
   * @param array $ids
   *   An array of contextual link ids.
   * @param string $current_path
   *   The Drupal path for the page for which the contextual links are rendered.
   *
   * @return string
   *   The response body.
   */
  protected function renderContextualLinks($ids, $current_path) {
    // Build POST values.
    $post = [];
    for ($i = 0; $i < count($ids); $i++) {
      $post['ids[' . $i . ']'] = $ids[$i];
    }

    // Serialize POST values.
    foreach ($post as $key => $value) {
      // Encode according to application/x-www-form-urlencoded
      // Both names and values needs to be urlencoded, according to
      // http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1
      $post[$key] = urlencode($key) . '=' . urlencode($value);
    }
    $post = implode('&', $post);

    // Perform HTTP request.
    return $this->curlExec([
      CURLOPT_URL => \Drupal::url('contextual.render', [], ['absolute' => TRUE, 'query' => ['destination' => $current_path]]),
      CURLOPT_POST => TRUE,
      CURLOPT_POSTFIELDS => $post,
      CURLOPT_HTTPHEADER => [
        'Accept: application/json',
        'Content-Type: application/x-www-form-urlencoded',
      ],
    ]);
  }

  /**
   * Tests if the node page works if Contextual Links is disabled.
   *
@@ -105,7 +34,7 @@ public function testPageWithDisabledContextualModule() {
    // Ensure that contextual links don't get called for admin users.
    $admin_user = User::load(1);
    $admin_user->setPassword('new_password');
    $admin_user->pass_raw = 'new_password';
    $admin_user->passRaw = 'new_password';
    $admin_user->save();

    $this->drupalCreateContentType(['type' => 'page']);
+117 −0
Changes for core/modules/node/tests/src/FunctionalJavascript/ContextualLinksTest.php: 117 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\node\FunctionalJavascript;

use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\node\Entity\Node;
use Drupal\Tests\contextual\FunctionalJavascript\ContextualLinkClickTrait;

/**
 * Create a node with revisions and test contextual links.
 *
 * @group node
 */
class ContextualLinksTest extends WebDriverTestBase {

  use ContextualLinkClickTrait;

  /**
   * An array of node revisions.
   *
   * @var \Drupal\node\NodeInterface[]
   */
  protected $nodes;


  /**
   * {@inheritdoc}
   */
  protected static $modules = ['node', 'contextual'];

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    $this->drupalCreateContentType([
      'type' => 'page',
      'name' => 'Basic page',
      'display_submitted' => FALSE,
    ]);

    // Create initial node.
    $node = $this->drupalCreateNode();

    $nodes = [];

    // Get original node.
    $nodes[] = clone $node;

    // Create two revisions.
    $revision_count = 2;
    for ($i = 0; $i < $revision_count; $i++) {

      // Create revision with a random title and body and update variables.
      $node->title = $this->randomMachineName();
      $node->body = [
        'value' => $this->randomMachineName(32),
        'format' => filter_default_format(),
      ];
      $node->setNewRevision();

      $node->save();

      // Make sure we get revision information.
      $node = Node::load($node->id());
      $nodes[] = clone $node;
    }

    $this->nodes = $nodes;

    $this->drupalLogin($this->createUser(
      [
        'view page revisions',
        'revert page revisions',
        'delete page revisions',
        'edit any page content',
        'delete any page content',
        'access contextual links',
        'administer content types',
      ]
    ));
  }

  /**
   * Tests the contextual links on revisions.
   */
  public function testRevisionContextualLinks() {
    // Confirm that the "Edit" and "Delete" contextual links appear for the
    // default revision.
    $this->drupalGet('node/' . $this->nodes[0]->id());
    $page = $this->getSession()->getPage();
    $page->waitFor(10, function () use ($page) {
      return $page->find('css', "main .contextual");
    });

    $this->toggleContextualTriggerVisibility('main');
    $page->find('css', 'main .contextual button')->press();
    $links = $page->findAll('css', "main .contextual-links li a");

    $this->assertEquals('Edit', $links[0]->getText());
    $this->assertEquals('Delete', $links[1]->getText());

    // Confirm that "Edit" and "Delete" contextual links don't appear for
    // non-default revision.
    $this->drupalGet("node/" . $this->nodes[0]->id() . "/revisions/" . $this->nodes[1]->getRevisionId() . "/view");
    $this->assertSession()->pageTextContains($this->nodes[1]->getTitle());
    $page->waitFor(10, function () use ($page) {
      return $page->find('css', "main .contextual");
    });

    $this->toggleContextualTriggerVisibility('main');
    $contextual_button = $page->find('css', 'main .contextual button');
    $this->assertEmpty(0, $contextual_button);
  }

}