comparison core/modules/search/tests/src/Functional/SearchTestBase.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 <?php
2
3 namespace Drupal\Tests\search\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\Component\Utility\SafeMarkup;
7
8 /**
9 * Defines the common search test code.
10 */
11 abstract class SearchTestBase extends BrowserTestBase {
12
13 /**
14 * Modules to enable.
15 *
16 * @var array
17 */
18 public static $modules = ['node', 'search', 'dblog'];
19
20 protected function setUp() {
21 parent::setUp();
22
23 // Create Basic page and Article node types.
24 if ($this->profile != 'standard') {
25 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
26 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
27 }
28 }
29
30 /**
31 * Simulates submission of a form using GET instead of POST.
32 *
33 * Forms that use the GET method cannot be submitted with
34 * WebTestBase::drupalPostForm(), which explicitly uses POST to submit the
35 * form. So this method finds the form, verifies that it has input fields and
36 * a submit button matching the inputs to this method, and then calls
37 * WebTestBase::drupalGet() to simulate the form submission to the 'action'
38 * URL of the form (if set, or the current URL if not).
39 *
40 * See WebTestBase::drupalPostForm() for more detailed documentation of the
41 * function parameters.
42 *
43 * @param string $path
44 * Location of the form to be submitted: either a Drupal path, absolute
45 * path, or NULL to use the current page.
46 * @param array $edit
47 * Form field data to submit. Unlike drupalPostForm(), this does not support
48 * file uploads.
49 * @param string $submit
50 * Value of the submit button to submit clicking. Unlike drupalPostForm(),
51 * this does not support AJAX.
52 * @param string $form_html_id
53 * (optional) HTML ID of the form, to disambiguate.
54 */
55 protected function submitGetForm($path, $edit, $submit, $form_html_id = NULL) {
56 if (isset($path)) {
57 $this->drupalGet($path);
58 }
59
60 if ($this->parse()) {
61 // Iterate over forms to find one that matches $edit and $submit.
62 $edit_save = $edit;
63 $xpath = '//form';
64 if (!empty($form_html_id)) {
65 $xpath .= "[@id='" . $form_html_id . "']";
66 }
67 $forms = $this->xpath($xpath);
68 foreach ($forms as $form) {
69 // Try to set the fields of this form as specified in $edit.
70 $edit = $edit_save;
71 $post = [];
72 $upload = [];
73 $submit_matches = $this->handleForm($post, $edit, $upload, $submit, $form);
74 if (!$edit && $submit_matches) {
75 // Everything matched, so "submit" the form.
76 $action = isset($form['action']) ? $this->getAbsoluteUrl((string) $form['action']) : NULL;
77 $this->drupalGet($action, ['query' => $post]);
78 return;
79 }
80 }
81
82 // We have not found a form which contained all fields of $edit and
83 // the submit button.
84 foreach ($edit as $name => $value) {
85 $this->fail(SafeMarkup::format('Failed to set field @name to @value', ['@name' => $name, '@value' => $value]));
86 }
87 $this->assertTrue($submit_matches, format_string('Found the @submit button', ['@submit' => $submit]));
88 $this->fail(format_string('Found the requested form fields at @path', ['@path' => $path]));
89 }
90 }
91
92 }