c7867221231aa2353f5ad522a859f1a7c67544c6
[yaffs-website] / web / core / modules / system / tests / src / Functional / Ajax / OffCanvasDialogTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Ajax;
4
5 use Drupal\ajax_test\Controller\AjaxTestController;
6 use Drupal\Component\Serialization\Json;
7 use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
8 use Drupal\Tests\BrowserTestBase;
9
10 /**
11  * Performs tests on opening and manipulating dialogs via AJAX commands.
12  *
13  * @group Ajax
14  */
15 class OffCanvasDialogTest extends BrowserTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['ajax_test'];
23
24   /**
25    * Test sending AJAX requests to open and manipulate off-canvas dialog.
26    *
27    * @dataProvider dialogPosition
28    */
29   public function testDialog($position) {
30     // Ensure the elements render without notices or exceptions.
31     $this->drupalGet('ajax-test/dialog');
32
33     // Set up variables for this test.
34     $dialog_renderable = AjaxTestController::dialogContents();
35     $dialog_contents = \Drupal::service('renderer')->renderRoot($dialog_renderable);
36
37     $off_canvas_expected_response = [
38       'command' => 'openDialog',
39       'selector' => '#drupal-off-canvas',
40       'settings' => NULL,
41       'data' => $dialog_contents,
42       'dialogOptions' =>
43         [
44           'title' => 'AJAX Dialog & contents',
45           'modal' => FALSE,
46           'autoResize' => FALSE,
47           'resizable' => 'w',
48           'draggable' => FALSE,
49           'drupalAutoButtons' => FALSE,
50           'drupalOffCanvasPosition' => $position ?: 'side',
51           'buttons' => [],
52           'dialogClass' => 'ui-dialog-off-canvas ui-dialog-position-' . ($position ?: 'side'),
53           'width' => 300,
54         ],
55       'effect' => 'fade',
56       'speed' => 1000,
57     ];
58
59     // Emulate going to the JS version of the page and check the JSON response.
60     $wrapper_format = $position && ($position !== 'side') ? 'drupal_dialog.off_canvas_' . $position : 'drupal_dialog.off_canvas';
61     $ajax_result = $this->drupalGet('ajax-test/dialog-contents', ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => $wrapper_format]]);
62     $ajax_result = Json::decode($ajax_result);
63     $this->assertEquals($off_canvas_expected_response, $ajax_result[3], 'off-canvas dialog JSON response matches.');
64   }
65
66   /**
67    * The data provider for potential dialog positions.
68    *
69    * @return array
70    */
71   public static function dialogPosition() {
72     return [
73       [NULL],
74       ['side'],
75       ['top'],
76     ];
77   }
78
79 }