3 namespace Drupal\Tests\metatag\Functional;
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 * Misc helper functions for the automated tests.
14 trait MetatagHelperTrait {
19 protected function loginUser1() {
21 /* @var \Drupal\user\Entity\User $account */
22 $account = User::load(1);
24 // Reset the password.
26 $account->setPassword($password)->save();
28 // Support old and new tests.
29 $account->passRaw = $password;
30 $account->pass_raw = $password;
33 $this->drupalLogin($account);
39 protected function verbose($message, $title = NULL) {
40 // Handle arrays, objects, etc.
41 if (!is_string($message)) {
42 $message = "<pre>\n" . print_r($message, TRUE) . "\n</pre>\n";
45 // Optional title to go before the output.
47 $title = '<h2>' . Html::escape($title) . "</h2>\n";
50 parent::verbose($title . $message);
54 * Create a content type and a node.
56 * @param string $title
57 * A title for the node that will be returned.
59 * The text to use as the body.
61 * @return \Drupal\node\NodeInterface
62 * A fully formatted node object.
64 private function createContentTypeNode($title = 'Title test', $body = 'Body test') {
65 $content_type = 'metatag_test';
67 'type' => $content_type,
68 'label' => 'Test content type',
70 $this->createContentType($args);
76 'format' => filter_default_format(),
80 'type' => $content_type,
83 return $this->createNode($args);
87 * Create a vocabulary.
89 * @param array $values
90 * Items passed to the vocabulary. If the 'vid' item is not present it will
91 * be automatically generated. If the 'name' item is not present the 'vid'
94 * @return \Drupal\taxonomy\Entity\Vocabulary
95 * A fully formatted vocabulary object.
97 private function createVocabulary(array $values = []) {
98 // Find a non-existent random type name.
99 if (!isset($values['vid'])) {
101 $id = strtolower($this->randomMachineName(8));
102 } while (Vocabulary::load($id));
105 $id = $values['vid'];
111 $vocab = Vocabulary::create($values);
112 $status = $vocab->save();
114 if ($this instanceof \PHPUnit_Framework_TestCase) {
115 $this->assertSame($status, SAVED_NEW, (new FormattableMarkup('Created vocabulary %type.', ['%type' => $vocab->id()]))->__toString());
118 $this->assertEqual($status, SAVED_NEW, (new FormattableMarkup('Created vocabulary %type.', ['%type' => $vocab->id()]))->__toString());
125 * Create a taxonomy term.
127 * @param array $values
128 * Items passed to the term. Requires the 'vid' element.
130 * @return Drupal\taxonomy\Entity\Term
131 * A fully formatted term object.
133 private function createTerm(array $values = []) {
134 // Populate defaults array.
138 'value' => $this->randomMachineName(32),
139 'format' => filter_default_format(),
142 'name' => $this->randomMachineName(8),
144 $term = Term::create($values);
145 $status = $term->save();
147 if ($this instanceof \PHPUnit_Framework_TestCase) {
148 $this->assertSame($status, SAVED_NEW, (new FormattableMarkup('Created term %name.', ['%name' => $term->label()]))->__toString());
151 $this->assertEqual($status, SAVED_NEW, (new FormattableMarkup('Created term %name.', ['%name' => $term->label()]))->__toString());