More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / modules / system / src / Tests / Common / RenderWebTest.php
1 <?php
2
3 namespace Drupal\system\Tests\Common;
4
5 use Drupal\Component\Serialization\Json;
6 use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
7 use Drupal\Core\Url;
8 use Drupal\simpletest\WebTestBase;
9
10 /**
11  * Performs integration tests on drupal_render().
12  *
13  * @group Common
14  */
15 class RenderWebTest extends WebTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['common_test'];
23
24   /**
25    * Asserts the cache context for the wrapper format is always present.
26    */
27   public function testWrapperFormatCacheContext() {
28     $this->drupalGet('common-test/type-link-active-class');
29     $this->assertIdentical(0, strpos($this->getRawContent(), "<!DOCTYPE html>\n<html"));
30     $this->assertIdentical('text/html; charset=UTF-8', $this->drupalGetHeader('Content-Type'));
31     $this->assertTitle('Test active link class | Drupal');
32     $this->assertCacheContext('url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT);
33
34     $this->drupalGet('common-test/type-link-active-class', ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'json']]);
35     $this->assertIdentical('application/json', $this->drupalGetHeader('Content-Type'));
36     $json = Json::decode($this->getRawContent());
37     $this->assertEqual(['content', 'title'], array_keys($json));
38     $this->assertIdentical('Test active link class', $json['title']);
39     $this->assertCacheContext('url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT);
40   }
41
42   /**
43    * Tests rendering form elements without passing through
44    * \Drupal::formBuilder()->doBuildForm().
45    */
46   public function testDrupalRenderFormElements() {
47     // Define a series of form elements.
48     $element = [
49       '#type' => 'button',
50       '#value' => $this->randomMachineName(),
51     ];
52     $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'submit']);
53
54     $element = [
55       '#type' => 'textfield',
56       '#title' => $this->randomMachineName(),
57       '#value' => $this->randomMachineName(),
58     ];
59     $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'text']);
60
61     $element = [
62       '#type' => 'password',
63       '#title' => $this->randomMachineName(),
64     ];
65     $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'password']);
66
67     $element = [
68       '#type' => 'textarea',
69       '#title' => $this->randomMachineName(),
70       '#value' => $this->randomMachineName(),
71     ];
72     $this->assertRenderedElement($element, '//textarea');
73
74     $element = [
75       '#type' => 'radio',
76       '#title' => $this->randomMachineName(),
77       '#value' => FALSE,
78     ];
79     $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'radio']);
80
81     $element = [
82       '#type' => 'checkbox',
83       '#title' => $this->randomMachineName(),
84     ];
85     $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'checkbox']);
86
87     $element = [
88       '#type' => 'select',
89       '#title' => $this->randomMachineName(),
90       '#options' => [
91         0 => $this->randomMachineName(),
92         1 => $this->randomMachineName(),
93       ],
94     ];
95     $this->assertRenderedElement($element, '//select');
96
97     $element = [
98       '#type' => 'file',
99       '#title' => $this->randomMachineName(),
100     ];
101     $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'file']);
102
103     $element = [
104       '#type' => 'item',
105       '#title' => $this->randomMachineName(),
106       '#markup' => $this->randomMachineName(),
107     ];
108     $this->assertRenderedElement($element, '//div[contains(@class, :class) and contains(., :markup)]/label[contains(., :label)]', [
109       ':class' => 'js-form-type-item',
110       ':markup' => $element['#markup'],
111       ':label' => $element['#title'],
112     ]);
113
114     $element = [
115       '#type' => 'hidden',
116       '#title' => $this->randomMachineName(),
117       '#value' => $this->randomMachineName(),
118     ];
119     $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'hidden']);
120
121     $element = [
122       '#type' => 'link',
123       '#title' => $this->randomMachineName(),
124       '#url' => Url::fromRoute('common_test.destination'),
125       '#options' => [
126         'absolute' => TRUE,
127       ],
128     ];
129     $this->assertRenderedElement($element, '//a[@href=:href and contains(., :title)]', [
130       ':href' => URL::fromRoute('common_test.destination')->setAbsolute()->toString(),
131       ':title' => $element['#title'],
132     ]);
133
134     $element = [
135       '#type' => 'details',
136       '#open' => TRUE,
137       '#title' => $this->randomMachineName(),
138     ];
139     $this->assertRenderedElement($element, '//details/summary[contains(., :title)]', [
140       ':title' => $element['#title'],
141     ]);
142
143     $element = [
144       '#type' => 'details',
145       '#open' => TRUE,
146       '#title' => $this->randomMachineName(),
147     ];
148     $this->assertRenderedElement($element, '//details');
149
150     $element['item'] = [
151       '#type' => 'item',
152       '#title' => $this->randomMachineName(),
153       '#markup' => $this->randomMachineName(),
154     ];
155     $this->assertRenderedElement($element, '//details/div/div[contains(@class, :class) and contains(., :markup)]', [
156       ':class' => 'js-form-type-item',
157       ':markup' => $element['item']['#markup'],
158     ]);
159   }
160
161   /**
162    * Tests that elements are rendered properly.
163    */
164   protected function assertRenderedElement(array $element, $xpath, array $xpath_args = []) {
165     $original_element = $element;
166     $this->setRawContent(drupal_render_root($element));
167     $this->verbose('<hr />' . $this->getRawContent());
168
169     // @see \Drupal\simpletest\WebTestBase::xpath()
170     $xpath = $this->buildXPathQuery($xpath, $xpath_args);
171     $element += ['#value' => NULL];
172     $this->assertFieldByXPath($xpath, $element['#value'], format_string('#type @type was properly rendered.', [
173       '@type' => var_export($element['#type'], TRUE),
174     ]));
175   }
176
177 }