Skip to content

Instantly share code, notes, and snippets.

@fmitchell
Created September 11, 2020 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fmitchell/ab51c05bcc5b94d6fdbb44bc5127e327 to your computer and use it in GitHub Desktop.
Save fmitchell/ab51c05bcc5b94d6fdbb44bc5127e327 to your computer and use it in GitHub Desktop.
Example Author editing article Drupal Test Trait test
<?php
namespace Drupal\Tests\sw_editorial\ExistingSiteJavascript;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
use weitzman\DrupalTestTraits\ExistingSiteWebDriverTestBase;
class AuthorManageArticleTest extends ExistingSiteWebDriverTestBase {
/**
* Create an article as an author.
*
* @throws \Behat\Mink\Exception\ExpectationException
* @throws \Behat\Mink\Exception\ResponseTextException
* @throws \Drupal\Core\Entity\EntityMalformedException
* @throws EntityStorageException
*/
public function testSaveDraft() {
// Gatsby module takes over display of pages. We want Drupal to display
// pages during this test.
// TODO: See if we can test nodes without uninstalling the Gatsby module.
\Drupal::service('module_installer')->uninstall(['gatsby']);
// Create an author user and login.
$author = $this->createAuthor();
$this->drupalLogin($author);
// Create an article node with a just title.
$node = $this->createNode([
'title' => 'Test Save Author',
'type' => 'article',
'uid' => $author->id(),
]);
$node->save();
// Confirm new node is owned by author.
$this->assertEquals($author->id(), $node->getOwnerId());
// Confirm we can see the node page and that it is in draft moderation.
$this->drupalGet($node->toUrl());
$web_assert = $this->assertSession();
$web_assert->statusCodeEquals(200);
$web_assert->pageTextContains('Test Save Author');
$web_assert->pageTextContains('Moderation state');
$web_assert->pageTextContains('Draft');
}
/**
* Edit an existing article and add content to fields.
*
* @throws \Behat\Mink\Exception\ResponseTextException
* @throws \Drupal\Core\Entity\EntityMalformedException
* @throws EntityStorageException
*/
public function testEditExistingDraft() {
// Create an author user and login.
$author = $this->createAuthor();
$this->drupalLogin($author);
$web_assert = $this->assertSession();
// Create an article node with a just title.
$node = $this->createNode([
'title' => 'Test Save Author',
'type' => 'article',
'uid' => $author->id(),
]);
$node->save();
// Edit
$this->drupalGet($node->toUrl('edit-form'));
$page = $this->getCurrentPage();
// Flags
$flag = $page->findField('field_flags[49]');
$flag->press();
// Health Topics
$topic = $page->findField('field_health_topic[target_id]');
$topic->setValue('All');
$topic->keyDown('e');
$result = $web_assert->waitForElementVisible('css', '.ui-autocomplete li');
$this->assertNotNull($result);
// Click the autocomplete option
$result->click();
// Verify that correct the input is selected.
$web_assert->pageTextContains('Allergies');
// Contributors
$cont_tab = $page->findLink('Contributors');
$cont_tab->press();
$dropbutton_toggle = $web_assert->elementExists('css', '#edit-field-paragraph-contributor-add-more .dropbutton-toggle button');
$dropbutton_toggle->click();
$paragraph_button = $page->findButton('Add new');
$paragraph_button->press();
$result = $web_assert->waitForElementVisible('css', '.field--name-field-first-name');
$this->assertNotNull($result);
$first_name = $page->findField('field_paragraph_contributor[0][subform][field_first_name][0][value]');
$first_name->setValue('First');
$last_name = $page->findField('field_paragraph_contributor[0][subform][field_last_name][0][value]');
$last_name->setValue('Last');
// Related Content
$rel_tab = $page->findLink('Related content');
$rel_tab->press();
$related = $page->findField('field_related[0][target_id]');
$related->setValue('Tes');
$related->keyDown('t');
$result = $web_assert->waitForElementVisible('css', '.ui-autocomplete li');
$this->assertNotNull($result);
// Click the autocomplete option
$result->click();
// Verify that correct the input is selected.
$web_assert->pageTextContains('Test Health Article');
// Content
$body_tab = $page->findLink('Body Content');
$body_tab->press();
$paragraph_button = $page->findButton('Add Generic Content Section');
$paragraph_button->press();
$result = $web_assert->waitForElementVisible('css', '.field--name-field-section-header');
$this->assertNotNull($result);
$section_header = $page->findField('field_body[0][subform][field_section_header][0][value]');
$section_header->setValue('Section Header');
// Save
$submit_button = $page->findButton('Save');
$submit_button->press();
}
public function testCannotPublish() {
// Create an author user and login.
$author = $this->createAuthor();
$this->drupalLogin($author);
$web_assert = $this->assertSession();
// Create an article node with a just title.
$node = $this->createNode([
'title' => 'Test Save Author',
'type' => 'article',
'uid' => $author->id(),
]);
$node->save();
//ensure that the publish option is not avaliable
$page = $this->getCurrentPage();
$publish_button = $page->findButton('publish');
$this->assertEqual($publish_button, 0);
}
/**
* Array of author permissions from the content author role.
*
* @return array
*/
protected function authorPermissions() {
return [
'create page content',
'create article content',
'use text format restricted_html',
'delete own page content',
'delete own article content',
'edit own page content',
'edit own article content',
'access content overview',
'view own unpublished content',
'access in-place editing',
'view the administration theme',
'create terms in symptoms',
'access toolbar',
];
}
/**
* Test Cannot Edit another user Draft
*
* @throws EntityStorageException
*/
public function testCannotEditAnotherUserDraft()
{
// Create an author user and login.
$author = $this->createAuthor();
$this->drupalLogin($author);
$web_assert = $this->assertSession();
// Create an diagnosis node with a just title.
$node = $this->createNode([
'title' => 'Test Save Author',
'type' => 'article',
'uid' => $author->id(),
]);
$node->save();
//Create a second user and login
$user = $this->createUser();
$this->drupalLogin($user);
$web_assert = $this->assertSession();
// Confirm new node is owned by author.
$this->assertEquals($author->id(), $node->getOwnerId());
//Confirm user does not own node
$this->assertNotEqual($user->id(), $node->getOwnerId());
}
/**
* An author cannot edit published diagnosis.
*
* @throws \Behat\Mink\Exception\ExpectationException
* @throws \Behat\Mink\Exception\ResponseTextException
* @throws \Drupal\Core\Entity\EntityMalformedException
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public function testReviewRequested() {
// Create an author user and login.
$author = $this->createAuthor();
$this->drupalLogin($author);
$web_assert = $this->assertSession();
// Create an Article node with a just title.
$node = $this->createNode([
'title' => 'Test Save Author Article',
'type' => 'article',
'uid' => $author->id(),
]);
$node->save();
//Ensure that the review requested is an option
$this->drupalGet($node->toUrl());
$web_assert = $this->assertSession();
$web_assert->statusCodeEquals(200);
$web_assert->pageTextContains('Review Requested');
}
/**
* Create the author role.
*
* @return User|false
* @throws EntityStorageException
*/
protected function createAuthor() {
$user = $this->createUser();
$role = Role::load('content_author');
$user->addRole($role->id());
$user->save();
return $user;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment