8dd93b3974b22cd2c2b8dd97e73d78cfa9f795f5
[yaffs-website] / web / modules / contrib / crop / src / Tests / CropFunctionalTest.php
1 <?php
2
3 namespace Drupal\crop\Tests;
4
5 use Drupal\Core\StreamWrapper\PublicStream;
6 use Drupal\crop\Entity\Crop;
7 use Drupal\crop\Entity\CropType;
8 use Drupal\file\Entity\File;
9 use Drupal\simpletest\WebTestBase;
10
11 /**
12  * Functional tests for crop API.
13  *
14  * @group crop
15  */
16 class CropFunctionalTest extends WebTestBase {
17
18   /**
19    * Modules to enable.
20    *
21    * @var array
22    */
23   public static $modules = ['crop', 'file'];
24
25   /**
26    * Admin user.
27    *
28    * @var \Drupal\user\UserInterface
29    */
30   protected $adminUser;
31
32   /**
33    * Test image style.
34    *
35    * @var \Drupal\image\ImageStyleInterface
36    */
37   protected $testStyle;
38
39   /**
40    * Test crop type.
41    *
42    * @var \Drupal\crop\CropInterface
43    */
44   protected $cropType;
45
46   /**
47    * {@inheritdoc}
48    */
49   protected function setUp() {
50     parent::setUp();
51
52     $this->adminUser = $this->drupalCreateUser(['administer crop types', 'administer image styles']);
53
54     // Create test image style.
55     $this->testStyle = $this->container->get('entity.manager')->getStorage('image_style')->create([
56       'name' => 'test',
57       'label' => 'Test image style',
58       'effects' => [],
59     ]);
60     $this->testStyle->save();
61   }
62
63   /**
64    * Tests crop type crud pages.
65    */
66   public function testCropTypeCrud() {
67     // Anonymous users don't have access to crop type admin pages.
68     $this->drupalGet('admin/config/media/crop');
69     $this->assertResponse(403);
70     $this->drupalGet('admin/config/media/crop/add');
71     $this->assertResponse(403);
72
73     // Can access pages if logged in and no crop types exist.
74     $this->drupalLogin($this->adminUser);
75     $this->drupalGet('admin/config/media/crop');
76     $this->assertResponse(200);
77     $this->assertText(t('No crop types available.'));
78     $this->assertLink(t('Add crop type'));
79
80     // Can access add crop type form.
81     $this->clickLink(t('Add crop type'));
82     $this->assertResponse(200);
83     $this->assertUrl('admin/config/media/crop/add');
84
85     // Create crop type.
86     $crop_type_id = strtolower($this->randomMachineName());
87     $edit = [
88       'id' => $crop_type_id,
89       'label' => $this->randomMachineName(),
90       'description' => $this->randomGenerator->sentences(10),
91     ];
92     $this->drupalPostForm('admin/config/media/crop/add', $edit, t('Save crop type'));
93     $this->assertRaw(t('The crop type %name has been added.', ['%name' => $edit['label']]));
94     $this->cropType = CropType::load($crop_type_id);
95     $this->assertUrl('admin/config/media/crop');
96     $label = $this->xpath("//td[contains(concat(' ',normalize-space(@class),' '),' menu-label ')]");
97     $this->assert(strpos($label[0]->asXML(), $edit['label']) !== FALSE, 'Crop type label found on listing page.');
98     $this->assertText($edit['description']);
99
100     // Check edit form.
101     $this->clickLink(t('Edit'));
102     $this->assertText(t('Edit @name crop type', ['@name' => $edit['label']]));
103     $this->assertRaw($edit['id']);
104     $this->assertFieldById('edit-label', $edit['label']);
105     $this->assertRaw($edit['description']);
106
107     // See if crop type appears on image effect configuration form.
108     $this->drupalGet('admin/config/media/image-styles/manage/' . $this->testStyle->id() . '/add/crop_crop');
109     $option = $this->xpath("//select[@id='edit-data-crop-type']/option");
110     $this->assert(strpos($option[0]->asXML(), $edit['label']) !== FALSE, 'Crop type label found on image effect page.');
111     $this->drupalPostForm('admin/config/media/image-styles/manage/' . $this->testStyle->id() . '/add/crop_crop', ['data[crop_type]' => $edit['id']], t('Add effect'));
112     $this->assertText(t('The image effect was successfully applied.'));
113     $this->assertText(t('Manual crop uses @name crop type', ['@name' => $edit['label']]));
114     $this->testStyle = $this->container->get('entity.manager')->getStorage('image_style')->loadUnchanged($this->testStyle->id());
115     $this->assertEqual($this->testStyle->getEffects()->count(), 1, 'One image effect added to test image style.');
116     $effect_configuration = $this->testStyle->getEffects()->getIterator()->current()->getConfiguration();
117     $this->assertEqual($effect_configuration['data'], ['crop_type' => $edit['id']], 'Manual crop effect uses correct image style.');
118
119     // Tests the image URI is extended with shortened hash in case of image
120     // style and corresponding crop existence.
121     $this->doTestFileUriAlter();
122
123     // Try to access edit form as anonymous user.
124     $this->drupalLogout();
125     $this->drupalGet('admin/config/media/crop/manage/' . $edit['id']);
126     $this->assertResponse(403);
127     $this->drupalLogin($this->adminUser);
128
129     // Try to create crop type with same machine name.
130     $this->drupalPostForm('admin/config/media/crop/add', $edit, t('Save crop type'));
131     $this->assertText(t('The machine-readable name is already in use. It must be unique.'));
132
133     // Delete crop type.
134     $this->drupalGet('admin/config/media/crop');
135     $this->assertLink('Test image style');
136     $this->clickLink(t('Delete'));
137     $this->assertText(t('Are you sure you want to delete the crop type @name?', ['@name' => $edit['label']]));
138     $this->drupalPostForm('admin/config/media/crop/manage/' . $edit['id'] . '/delete', [], t('Delete'));
139     $this->assertRaw(t('The crop type %name has been deleted.', ['%name' => $edit['label']]));
140     $this->assertText(t('No crop types available.'));
141   }
142
143   /**
144    * Asserts a shortened hash is added to the file URI.
145    *
146    * Tests crop_file_url_alter().
147    */
148   protected function doTestFileUriAlter() {
149     // Get the test file.
150     file_unmanaged_copy(drupal_get_path('module', 'crop') . '/tests/files/sarajevo.png', PublicStream::basePath());
151     $file_uri = 'public://sarajevo.png';
152     $file = File::create(['uri' => $file_uri, 'status' => FILE_STATUS_PERMANENT]);
153     $file->save();
154
155     /** @var \Drupal\crop\CropInterface $crop */
156     $values = [
157       'type' => $this->cropType->id(),
158       'entity_id' => $file->id(),
159       'entity_type' => $file->getEntityTypeId(),
160       'uri' => 'public://sarajevo.png',
161       'x' => '100',
162       'y' => '150',
163       'width' => '200',
164       'height' => '250',
165     ];
166     $crop = Crop::create($values);
167     $crop->save();
168
169     // Build an image style derivative for the file URI.
170     $image_style_uri = $this->testStyle->buildUri($file_uri);
171     // Build an image style URL.
172     $image_style_url = $this->testStyle->buildUrl($image_style_uri);
173     // This triggers crop_file_url_alter().
174     $altered_image_style_url = file_create_url($image_style_url);
175
176     $shortened_hash = substr(md5(implode($crop->position()) . implode($crop->anchor())), 0, 8);
177     $this->assertTrue(strpos($altered_image_style_url, $shortened_hash) !== FALSE, 'The image style URL contains a shortened hash.');
178
179     // Update the crop to assert the hash has changed.
180     $crop->setPosition('80', '80')->save();
181     $old_hash = $shortened_hash;
182     $new_hash = substr(md5(implode($crop->position()) . implode($crop->anchor())), 0, 8);
183     $altered_image_style_url = file_create_url($image_style_url);
184     $this->assertFalse(strpos($altered_image_style_url, $old_hash) !== FALSE, 'The image style URL does not contain the old hash.');
185     $this->assertTrue(strpos($altered_image_style_url, $new_hash) !== FALSE, 'The image style URL contains an updated hash.');
186
187     // Delete the file and the crop entity associated,
188     // the crop entity are auto cleaned by crop_file_delete().
189     $file->delete();
190
191     // Check if crop entity are correctly deleted.
192     $this->assertFalse(Crop::cropExists($file_uri), 'The Crop entity are correctly deleted after file delete.');
193   }
194
195 }