3 namespace Drupal\metatag\Tests;
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\Component\Utility\Html;
7 use Drupal\taxonomy\Entity\Term;
8 use Drupal\taxonomy\Entity\Vocabulary;
9 use Drupal\user\Entity\User;
12 * A copy of Drupal\Tests\metatag\Functional\MetatagHelperTrait.
14 * @todo Remove once the other tests are converted over.
16 trait MetatagHelperTrait {
21 protected function loginUser1() {
23 /* @var \Drupal\user\Entity\User $account */
24 $account = User::load(1);
26 // Reset the password.
28 $account->setPassword($password)->save();
30 // Support old and new tests.
31 $account->passRaw = $password;
32 $account->pass_raw = $password;
35 $this->drupalLogin($account);
41 protected function verbose($message, $title = NULL) {
42 // Handle arrays, objects, etc.
43 if (!is_string($message)) {
44 $message = "<pre>\n" . print_r($message, TRUE) . "\n</pre>\n";
47 // Optional title to go before the output.
49 $title = '<h2>' . Html::escape($title) . "</h2>\n";
52 parent::verbose($title . $message);
56 * Create a content type and a node.
58 * @param string $title
59 * A title for the node that will be returned.
61 * The text to use as the body.
63 * @return \Drupal\node\NodeInterface
64 * A fully formatted node object.
66 private function createContentTypeNode($title = 'Title test', $body = 'Body test') {
67 $content_type = 'metatag_test';
69 'type' => $content_type,
70 'label' => 'Test content type',
72 $this->createContentType($args);
78 'format' => filter_default_format(),
82 'type' => $content_type,
85 return $this->createNode($args);
89 * Create a vocabulary.
91 * @param array $values
92 * Items passed to the vocabulary. If the 'vid' item is not present it will
93 * be automatically generated. If the 'name' item is not present the 'vid'
96 * @return Drupal\taxonomy\Entity\Vocabulary
97 * A fully formatted vocabulary object.
99 private function createVocabulary(array $values = []) {
100 // Find a non-existent random type name.
101 if (!isset($values['vid'])) {
103 $id = strtolower($this->randomMachineName(8));
104 } while (Vocabulary::load($id));
107 $id = $values['vid'];
113 $vocab = Vocabulary::create($values);
114 $status = $vocab->save();
116 if ($this instanceof \PHPUnit_Framework_TestCase) {
117 $this->assertSame($status, SAVED_NEW, (new FormattableMarkup('Created vocabulary %type.', ['%type' => $vocab->id()]))->__toString());
120 $this->assertEqual($status, SAVED_NEW, (new FormattableMarkup('Created vocabulary %type.', ['%type' => $vocab->id()]))->__toString());
127 * Create a taxonomy term.
129 * @param array $values
130 * Items passed to the term. Requires the 'vid' element.
132 * @return Drupal\taxonomy\Entity\Term
133 * A fully formatted term object.
135 private function createTerm(array $values = []) {
136 // Populate defaults array.
140 'value' => $this->randomMachineName(32),
141 'format' => filter_default_format(),
144 'name' => $this->randomMachineName(8),
146 $term = Term::create($values);
147 $status = $term->save();
149 if ($this instanceof \PHPUnit_Framework_TestCase) {
150 $this->assertSame($status, SAVED_NEW, (new FormattableMarkup('Created term %name.', ['%name' => $term->label()]))->__toString());
153 $this->assertEqual($status, SAVED_NEW, (new FormattableMarkup('Created term %name.', ['%name' => $term->label()]))->__toString());