Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / metatag / tests / src / Functional / MetatagHelperTrait.php
1 <?php
2
3 namespace Drupal\Tests\metatag\Functional;
4
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;
10
11 /**
12  * Misc helper functions for the automated tests.
13  */
14 trait MetatagHelperTrait {
15
16   /**
17    * Log in as user 1.
18    */
19   protected function loginUser1() {
20     // Load user 1.
21     /* @var \Drupal\user\Entity\User $account */
22     $account = User::load(1);
23
24     // Reset the password.
25     $password = 'foo';
26     $account->setPassword($password)->save();
27
28     // Support old and new tests.
29     $account->passRaw = $password;
30     $account->pass_raw = $password;
31
32     // Login.
33     $this->drupalLogin($account);
34   }
35
36   /**
37    * {@inheritdoc}
38    */
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";
43     }
44
45     // Optional title to go before the output.
46     if (!empty($title)) {
47       $title = '<h2>' . Html::escape($title) . "</h2>\n";
48     }
49
50     parent::verbose($title . $message);
51   }
52
53   /**
54    * Create a content type and a node.
55    *
56    * @param string $title
57    *   A title for the node that will be returned.
58    * @param string $body
59    *   The text to use as the body.
60    *
61    * @return \Drupal\node\NodeInterface
62    *   A fully formatted node object.
63    */
64   private function createContentTypeNode($title = 'Title test', $body = 'Body test') {
65     $content_type = 'metatag_test';
66     $args = [
67       'type' => $content_type,
68       'label' => 'Test content type',
69     ];
70     $this->createContentType($args);
71
72     $args = [
73       'body' => [
74         [
75           'value' => $body,
76           'format' => filter_default_format(),
77         ],
78       ],
79       'title' => $title,
80       'type' => $content_type,
81     ];
82
83     return $this->createNode($args);
84   }
85
86   /**
87    * Create a vocabulary.
88    *
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'
92    *   will be used.
93    *
94    * @return \Drupal\taxonomy\Entity\Vocabulary
95    *   A fully formatted vocabulary object.
96    */
97   private function createVocabulary(array $values = []) {
98     // Find a non-existent random type name.
99     if (!isset($values['vid'])) {
100       do {
101         $id = strtolower($this->randomMachineName(8));
102       } while (Vocabulary::load($id));
103     }
104     else {
105       $id = $values['vid'];
106     }
107     $values += [
108       'vid' => $id,
109       'name' => $id,
110     ];
111     $vocab = Vocabulary::create($values);
112     $status = $vocab->save();
113
114     if ($this instanceof \PHPUnit_Framework_TestCase) {
115       $this->assertSame($status, SAVED_NEW, (new FormattableMarkup('Created vocabulary %type.', ['%type' => $vocab->id()]))->__toString());
116     }
117     else {
118       $this->assertEqual($status, SAVED_NEW, (new FormattableMarkup('Created vocabulary %type.', ['%type' => $vocab->id()]))->__toString());
119     }
120
121     return $vocab;
122   }
123
124   /**
125    * Create a taxonomy term.
126    *
127    * @param array $values
128    *   Items passed to the term. Requires the 'vid' element.
129    *
130    * @return Drupal\taxonomy\Entity\Term
131    *   A fully formatted term object.
132    */
133   private function createTerm(array $values = []) {
134     // Populate defaults array.
135     $values += [
136       'description' => [
137         [
138           'value' => $this->randomMachineName(32),
139           'format' => filter_default_format(),
140         ],
141       ],
142       'name' => $this->randomMachineName(8),
143     ];
144     $term = Term::create($values);
145     $status = $term->save();
146
147     if ($this instanceof \PHPUnit_Framework_TestCase) {
148       $this->assertSame($status, SAVED_NEW, (new FormattableMarkup('Created term %name.', ['%name' => $term->label()]))->__toString());
149     }
150     else {
151       $this->assertEqual($status, SAVED_NEW, (new FormattableMarkup('Created term %name.', ['%name' => $term->label()]))->__toString());
152     }
153
154     return $term;
155   }
156
157 }