Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / image_widget_crop / src / Tests / ImageWidgetCropTest.php
1 <?php
2
3 namespace Drupal\image_widget_crop\Tests;
4
5 use Drupal\node\Entity\Node;
6 use Drupal\simpletest\WebTestBase;
7
8 /**
9  * Minimal test case for the image_widget_crop module.
10  *
11  * @group image_widget_crop
12  *
13  * @ingroup media
14  */
15 class ImageWidgetCropTest extends WebTestBase {
16
17   /**
18    * User with permissions to create content.
19    *
20    * @var \Drupal\user\Entity\User
21    */
22   protected $user;
23
24   /**
25    * Modules to enable.
26    *
27    * @var array
28    */
29   public static $modules = [
30     'node',
31     'crop',
32     'image',
33     'image_widget_crop',
34   ];
35
36   /**
37    * Prepares environment for the tests.
38    */
39   protected function setUp() {
40     parent::setUp();
41
42     $this->drupalCreateContentType(['name' => 'Crop test', 'type' => 'crop_test']);
43
44     $this->user = $this->createUser([
45       'access content overview',
46       'administer content types',
47       'edit any crop_test content',
48     ]);
49     $this->drupalLogin($this->user);
50   }
51
52   /**
53    * Test Image Widget Crop UI.
54    */
55   public function testCropUi() {
56     // Test that when a crop has more than one usage we have a warning.
57     $this->createImageField('field_image_crop_test', 'crop_test', 'image_widget_crop', [], [], ['crop_list' => ['crop_16_9' => 'crop_16_9'], 'crop_types_required' => []]);
58     $this->drupalGetTestFiles('image');
59
60     $this->drupalGet('node/add/crop_test');
61     $edit = [
62       'title[0][value]' => $this->randomMachineName(),
63       'files[field_image_crop_test_0]' => \Drupal::service('file_system')->realpath('public://image-test.jpg'),
64     ];
65     $this->drupalPostAjaxForm(NULL, $edit, $this->getButtonName('//input[@type="submit" and @value="Upload" and @data-drupal-selector="edit-field-image-crop-test-0-upload-button"]'));
66
67     $node = Node::create([
68       'title' => '2nd node using it',
69       'type' => 'crop_test',
70       'field_image_crop_test' => 1,
71       'alt' => $this->randomMachineName(),
72     ]);
73     $node->save();
74
75     /** @var \Drupal\file\FileUsage\FileUsageInterface $usage */
76     $usage = \Drupal::service('file.usage');
77     $usage->add(\Drupal::service('entity_type.manager')->getStorage('file')->load(1), 'image_widget_crop', 'node', $node->id());
78
79     $this->drupalGet('node/1/edit');
80
81     $this->assertRaw(t('This crop definition affects more usages of this image'));
82
83   }
84
85   /**
86    * Test Image Widget Crop.
87    */
88   public function testImageWidgetCrop() {
89     // Test that crop widget works properly.
90     $this->createImageField('field_image_crop_test', 'crop_test', 'image_widget_crop', [], [], ['crop_list' => ['crop_16_9' => 'crop_16_9'], 'crop_types_required' => []]);
91     $this->drupalGetTestFiles('image');
92
93     $this->drupalGet('node/add/crop_test');
94
95     // Assert that there is no crop widget, neither 'Alternative text' text
96     // filed nor 'Remove' button yet.
97     $raw = '<summary role="button" aria-controls="edit-field-image-crop-test-0-image-crop-crop-wrapper" aria-expanded="false" aria-pressed="false">Crop image</summary>';
98     $this->assertNoRaw($raw);
99     $this->assertNoText('Alternative text');
100     $this->assertNoFieldByName('field_image_crop_test_0_remove_button');
101
102     $image = [];
103     // Upload an image in field_image_crop_test_0.
104     $image['files[field_image_crop_test_0]'] = $this->container->get('file_system')->realpath('public://image-test.jpg');
105     $this->drupalPostAjaxForm(NULL, $image, $this->getButtonName('//input[@type="submit" and @value="Upload" and @data-drupal-selector="edit-field-image-crop-test-0-upload-button"]'));
106
107     // Assert that now crop widget and 'Alternative text' text field appear and
108     // that 'Remove' button exists.
109     $this->assertRaw($raw);
110     $this->assertText('Alternative text');
111     $this->assertFieldByName('field_image_crop_test_0_remove_button');
112
113     // Set title and 'Alternative text' text field and save.
114     $title = $this->randomMachineName();
115     $edit = [
116       'title[0][value]' => $title,
117       'field_image_crop_test[0][alt]' => $this->randomMachineName(),
118     ];
119     $this->drupalPostForm(NULL, $edit, 'Save');
120     $this->assertText('Crop test ' . $title . ' has been created.');
121     $url = $this->getUrl();
122     $nid = substr($url, -1, strrpos($url, '/'));
123
124     // Edit crop image.
125     $this->drupalGet('node/' . $nid . '/edit');
126
127     // Verify that the 'Remove' button works properly.
128     $this->assertText('Alternative text');
129     $this->drupalPostForm(NULL, NULL, 'Remove');
130     $this->assertNoText('Alternative text');
131
132     // Re-upload the image and set the 'Alternative text'.
133     $this->drupalPostAjaxForm(NULL, $image, $this->getButtonName('//input[@type="submit" and @value="Upload" and @data-drupal-selector="edit-field-image-crop-test-0-upload-button"]'));
134
135     // Verify that the 'Preview' button works properly.
136     $this->drupalPostForm(NULL, $edit, 'Preview');
137     $this->assertLink('Back to content editing');
138     $this->clickLink('Back to content editing');
139
140     // Verify that there is an image style preview.
141     $this->assertFieldByName('field_image_crop_test[0][width]', '40');
142     $this->assertFieldByName('field_image_crop_test[0][height]', '20');
143
144   }
145
146   /**
147    * Gets IEF button name.
148    *
149    * @param string $xpath
150    *   Xpath of the button.
151    *
152    * @return string
153    *   The name of the button.
154    */
155   protected function getButtonName($xpath) {
156     $retval = '';
157
158     /** @var \SimpleXMLElement[] $elements */
159     if ($elements = $this->xpath($xpath)) {
160       foreach ($elements[0]->attributes() as $name => $value) {
161         if ($name == 'name') {
162           $retval = (string) $value;
163           break;
164         }
165       }
166     }
167     return $retval;
168   }
169
170   /**
171    * Create a new image field.
172    *
173    * @param string $name
174    *   The name of the new field (all lowercase), exclude the "field_" prefix.
175    * @param string $type_name
176    *   The node type that this field will be added to.
177    * @param string $widget_name
178    *   The name of the widget.
179    * @param array $storage_settings
180    *   A list of field storage settings that will be added to the defaults.
181    * @param array $field_settings
182    *   A list of instance settings that will be added to the instance defaults.
183    * @param array $widget_settings
184    *   A list of widget settings that will be added to the widget defaults.
185    */
186   protected function createImageField($name, $type_name, $widget_name, array $storage_settings = [], array $field_settings = [], array $widget_settings = []) {
187     \Drupal::entityTypeManager()->getStorage('field_storage_config')->create([
188       'field_name' => $name,
189       'entity_type' => 'node',
190       'type' => 'image',
191       'settings' => $storage_settings,
192       'cardinality' => !empty($storage_settings['cardinality']) ? $storage_settings['cardinality'] : 1,
193     ])->save();
194
195     $field_config = \Drupal::entityTypeManager()->getStorage('field_config')->create([
196       'field_name' => $name,
197       'label' => $name,
198       'entity_type' => 'node',
199       'bundle' => $type_name,
200       'required' => !empty($field_settings['required']),
201       'settings' => $field_settings,
202     ]);
203     $field_config->save();
204
205     /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
206     $form_display = \Drupal::entityTypeManager()->getStorage('entity_form_display')->load('node.' . $type_name . '.default');
207     $form_display->setComponent($name, [
208       'type' => $widget_name,
209       'settings' => $widget_settings,
210     ])->save();
211
212     /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $view_display */
213     $view_display = \Drupal::entityTypeManager()->getStorage('entity_view_display')->load('node.' . $type_name . '.default');
214     $view_display->setComponent($name)
215       ->save();
216
217   }
218
219 }