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