6500d1adefd0c388b61ad3179b11626f98078c4a
[yaffs-website] / web / core / modules / editor / tests / src / Kernel / EditorImageDialogTest.php
1 <?php
2
3 namespace Drupal\Tests\editor\Kernel;
4
5 use Drupal\Core\Form\FormState;
6 use Drupal\editor\Entity\Editor;
7 use Drupal\editor\Form\EditorImageDialog;
8 use Drupal\filter\Entity\FilterFormat;
9 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
10 use Drupal\node\Entity\NodeType;
11
12 /**
13  * Tests EditorImageDialog validation and conversion functionality.
14  *
15  * @group editor
16  */
17 class EditorImageDialogTest extends EntityKernelTestBase {
18
19   /**
20    * Text editor config entity for testing.
21    *
22    * @var \Drupal\editor\EditorInterface
23    */
24   protected $editor;
25
26   /**
27    * Modules to enable.
28    *
29    * @var array
30    */
31   public static $modules = ['node', 'file', 'editor', 'editor_test', 'user', 'system'];
32
33   /**
34    * Sets up the test.
35    */
36   protected function setUp() {
37     parent::setUp();
38     $this->installEntitySchema('file');
39     $this->installSchema('system', ['key_value_expire']);
40     $this->installSchema('node', ['node_access']);
41     $this->installSchema('file', ['file_usage']);
42     $this->installConfig(['node']);
43
44     // Add text formats.
45     $format = FilterFormat::create([
46       'format' => 'filtered_html',
47       'name' => 'Filtered HTML',
48       'weight' => 0,
49       'filters' => [
50         'filter_align' => ['status' => TRUE],
51         'filter_caption' => ['status' => TRUE],
52       ],
53     ]);
54     $format->save();
55
56     // Set up text editor.
57     $editor = Editor::create([
58       'format' => 'filtered_html',
59       'editor' => 'unicorn',
60       'image_upload' => [
61         'max_size' => 100,
62         'scheme' => 'public',
63         'directory' => '',
64         'status' => TRUE,
65       ],
66     ]);
67     $editor->save();
68     $this->editor = $editor;
69
70     // Create a node type for testing.
71     $type = NodeType::create(['type' => 'page', 'name' => 'page']);
72     $type->save();
73     node_add_body_field($type);
74     $this->installEntitySchema('user');
75     \Drupal::service('router.builder')->rebuild();
76   }
77
78   /**
79    * Tests that editor image dialog works as expected.
80    */
81   public function testEditorImageDialog() {
82     $input = [
83       'editor_object' => [
84         'src' => '/sites/default/files/inline-images/somefile.png',
85         'alt' => 'fda',
86         'width' => '',
87         'height' => '',
88         'data-entity-type' => 'file',
89         'data-entity-uuid' => 'some-uuid',
90         'data-align' => 'none',
91         'hasCaption' => 'false',
92       ],
93       'dialogOptions' => [
94         'title' => 'Edit Image',
95         'dialogClass' => 'editor-image-dialog',
96         'autoResize' => 'true',
97       ],
98       '_drupal_ajax' => '1',
99       'ajax_page_state' => [
100         'theme' => 'bartik',
101         'theme_token' => 'some-token',
102         'libraries' => '',
103       ],
104     ];
105     $form_state = (new FormState())
106       ->setRequestMethod('POST')
107       ->setUserInput($input)
108       ->addBuildInfo('args', [$this->editor]);
109
110     $form_builder = $this->container->get('form_builder');
111     $form_object = new EditorImageDialog(\Drupal::entityManager()->getStorage('file'));
112     $form_id = $form_builder->getFormId($form_object, $form_state);
113     $form = $form_builder->retrieveForm($form_id, $form_state);
114     $form_builder->prepareForm($form_id, $form, $form_state);
115     $form_builder->processForm($form_id, $form, $form_state);
116
117     // Assert these two values are present and we don't get the 'not-this'
118     // default back.
119     $this->assertEqual(FALSE, $form_state->getValue(['attributes', 'hasCaption'], 'not-this'));
120   }
121
122 }