Skip to content

Instantly share code, notes, and snippets.

@weitzman
Last active January 22, 2019 18:52
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 weitzman/0855b43fe751b7c96f8b33ed71d884d3 to your computer and use it in GitHub Desktop.
Save weitzman/0855b43fe751b7c96f8b33ed71d884d3 to your computer and use it in GitHub Desktop.
A test class that leverages Drupal Test Traits
<?php
// Use your module's testing namespace such as the one below.
namespace Drupal\Tests\moduleName\ExistingSite;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\user\Entity\User;
use weitzman\DrupalTestTraits\ExistingSiteBase;
/**
* A model test case using traits from Drupal Test Traits.
*/
class ExampleTest extends ExistingSiteBase
{
/**
* An example test method; note that Drupal API's and Mink are available.
*/
public function testLlama()
{
// Creates a user. Will be automatically cleaned up at the end of the test.
$author = $this->createUser([], null, true);
// Create a taxonomy term. Will be automatically cleaned up at the end of the test.
$vocab = Vocabulary::load('tags');
$term = $this->createTerm($vocab);
// Create a "Llama" article. Will be automatically cleaned up at end of test.
$node = $this->createNode([
'title' => 'Llama',
'type' => 'article',
'field_tags' => [
'target_id' => $term->id(),
],
'uid' => $author->id(),
]);
$node->setPublished()->save();
$this->assertEquals($author->id(), $node->getOwnerId());
// We can browse pages.
$this->drupalGet($node->toUrl());
$this->assertSession()->statusCodeEquals(200);
$this->drupalGet($node->toUrl());
$this->assertSession()->pageTextContains($site_name);
// We can login and browse admin pages.
$this->drupalLogin($author);
$this->drupalGet($node->toUrl('edit-form'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment