|
10 | 10 | use Drupal\Core\Language\LanguageInterface; |
11 | 11 | use Drupal\Core\Site\Settings; |
12 | 12 | use Drupal\file_entity\Entity\FileEntity; |
| 13 | +use Drupal\Core\Archiver\Tar; |
13 | 14 |
|
14 | 15 | /** |
15 | 16 | * Tests creating and saving a file. |
@@ -136,4 +137,61 @@ public function testImageAltTitleFields() { |
136 | 137 | $this->assertEqual($value, $created_file->get($field)->value); |
137 | 138 | } |
138 | 139 | } |
| 140 | + |
| 141 | + /** |
| 142 | + * Test archive upload. |
| 143 | + */ |
| 144 | + public function testArchiveUpload() { |
| 145 | + // Create files for the archive. |
| 146 | + file_unmanaged_save_data($this->randomString(), 'temporary://test_text.txt'); |
| 147 | + file_unmanaged_save_data($this->randomString(), 'temporary://test_png.png'); |
| 148 | + file_unmanaged_save_data($this->randomString(), 'temporary://test_jpg.jpg'); |
| 149 | + |
| 150 | + $archive_uri = file_unmanaged_save_data(NULL, 'temporary://archive.tar.gz'); |
| 151 | + |
| 152 | + $archive_path = file_directory_temp() . '/' . file_uri_target($archive_uri); |
| 153 | + $archiver = new Tar($archive_path); |
| 154 | + $archiver->add('test_text.txt test_png.png test_jpg.jpg'); |
| 155 | + |
| 156 | + $edit = [ |
| 157 | + 'files[upload]' => $archive_path, |
| 158 | + 'pattern' => '.*jpg|.*gif', |
| 159 | + 'remove_archive' => TRUE, |
| 160 | + ]; |
| 161 | + $this->drupalGet('admin/content/files/archive'); |
| 162 | + $this->drupalPostForm(NULL, $edit, t('Submit')); |
| 163 | + |
| 164 | + $this->assertText('Extracted archive.tar.gz and added 1 new files.'); |
| 165 | + |
| 166 | + // File that match the pattern can be found in the database. |
| 167 | + $this->assertEqual('test_jpg.jpg', $this->getFileByFilename('test_jpg.jpg')->getFilename()); |
| 168 | + // Files that don't match the pattern are not in the database. |
| 169 | + $this->assertFalse($this->getFileByFilename('test_png.png')); |
| 170 | + $this->assertFalse($this->getFileByFilename('test_text.txt')); |
| 171 | + // Archive is removed since we checked the remove_archive checkbox. |
| 172 | + $this->assertFalse($this->getFileByFilename('archive.tar.gz')); |
| 173 | + |
| 174 | + $all_files = file_scan_directory('public://', '/.*/'); |
| 175 | + // Only files that match the pattern are in the public directory. |
| 176 | + $this->assertTrue(array_key_exists('public://archive.tar/test_jpg.jpg', $all_files)); |
| 177 | + $this->assertFalse(array_key_exists('public://archive.tar/test_png.png', $all_files)); |
| 178 | + $this->assertFalse(array_key_exists('public://archive.tar/test_text.txt', $all_files)); |
| 179 | + $this->assertFalse(array_key_exists('public://archive.tar.gz', $all_files)); |
| 180 | + |
| 181 | + $archive_uri = file_unmanaged_save_data(NULL, 'temporary://archive2.tar.gz'); |
| 182 | + $archive_path = file_directory_temp() . '/' . file_uri_target($archive_uri); |
| 183 | + $edit = [ |
| 184 | + 'files[upload]' => $archive_path, |
| 185 | + 'remove_archive' => FALSE, |
| 186 | + ]; |
| 187 | + $this->drupalGet('admin/content/files/archive'); |
| 188 | + $this->drupalPostForm(NULL, $edit, t('Submit')); |
| 189 | + |
| 190 | + // Archive is in the database since value for remove_checkbox is FALSE. |
| 191 | + $this->assertEqual('archive2.tar.gz', $this->getFileByFilename('archive2.tar.gz')->getFilename()); |
| 192 | + |
| 193 | + $all_files = file_scan_directory('public://', '/.*/'); |
| 194 | + $this->assertTrue(array_key_exists('public://archive2.tar.gz', $all_files)); |
| 195 | + } |
| 196 | + |
139 | 197 | } |
0 commit comments