Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/file/tests/src/Kernel/UsageTest.php @ 5:12f9dff5fda9 tip
Update to Drupal core 8.7.1
author | Chris Cannam |
---|---|
date | Thu, 09 May 2019 15:34:47 +0100 |
parents | a9cd425dd02b |
children |
comparison
equal
deleted
inserted
replaced
4:a9cd425dd02b | 5:12f9dff5fda9 |
---|---|
1 <?php | 1 <?php |
2 | 2 |
3 namespace Drupal\Tests\file\Kernel; | 3 namespace Drupal\Tests\file\Kernel; |
4 | 4 |
5 use Drupal\Core\Database\Database; | |
5 use Drupal\field\Entity\FieldConfig; | 6 use Drupal\field\Entity\FieldConfig; |
6 use Drupal\field\Entity\FieldStorageConfig; | 7 use Drupal\field\Entity\FieldStorageConfig; |
7 use Drupal\language\Entity\ConfigurableLanguage; | 8 use Drupal\language\Entity\ConfigurableLanguage; |
8 use Drupal\language\Entity\ContentLanguageSettings; | 9 use Drupal\language\Entity\ContentLanguageSettings; |
9 use Drupal\node\Entity\Node; | 10 use Drupal\node\Entity\Node; |
19 /** | 20 /** |
20 * Tests \Drupal\file\FileUsage\DatabaseFileUsageBackend::listUsage(). | 21 * Tests \Drupal\file\FileUsage\DatabaseFileUsageBackend::listUsage(). |
21 */ | 22 */ |
22 public function testGetUsage() { | 23 public function testGetUsage() { |
23 $file = $this->createFile(); | 24 $file = $this->createFile(); |
24 db_insert('file_usage') | 25 $connection = Database::getConnection(); |
26 $connection->insert('file_usage') | |
25 ->fields([ | 27 ->fields([ |
26 'fid' => $file->id(), | 28 'fid' => $file->id(), |
27 'module' => 'testing', | 29 'module' => 'testing', |
28 'type' => 'foo', | 30 'type' => 'foo', |
29 'id' => 1, | 31 'id' => 1, |
30 'count' => 1, | 32 'count' => 1, |
31 ]) | 33 ]) |
32 ->execute(); | 34 ->execute(); |
33 db_insert('file_usage') | 35 $connection->insert('file_usage') |
34 ->fields([ | 36 ->fields([ |
35 'fid' => $file->id(), | 37 'fid' => $file->id(), |
36 'module' => 'testing', | 38 'module' => 'testing', |
37 'type' => 'bar', | 39 'type' => 'bar', |
38 'id' => 2, | 40 'id' => 2, |
59 // Add the file twice to ensure that the count is incremented rather than | 61 // Add the file twice to ensure that the count is incremented rather than |
60 // creating additional records. | 62 // creating additional records. |
61 $file_usage->add($file, 'testing', 'bar', 2); | 63 $file_usage->add($file, 'testing', 'bar', 2); |
62 $file_usage->add($file, 'testing', 'bar', 2); | 64 $file_usage->add($file, 'testing', 'bar', 2); |
63 | 65 |
64 $usage = db_select('file_usage', 'f') | 66 $usage = Database::getConnection()->select('file_usage', 'f') |
65 ->fields('f') | 67 ->fields('f') |
66 ->condition('f.fid', $file->id()) | 68 ->condition('f.fid', $file->id()) |
67 ->execute() | 69 ->execute() |
68 ->fetchAllAssoc('id'); | 70 ->fetchAllAssoc('id'); |
69 $this->assertEqual(count($usage), 2, 'Created two records'); | 71 $this->assertEqual(count($usage), 2, 'Created two records'); |
102 */ | 104 */ |
103 public function doTestRemoveUsage() { | 105 public function doTestRemoveUsage() { |
104 $file = $this->createFile(); | 106 $file = $this->createFile(); |
105 $file->setPermanent(); | 107 $file->setPermanent(); |
106 $file_usage = $this->container->get('file.usage'); | 108 $file_usage = $this->container->get('file.usage'); |
107 db_insert('file_usage') | 109 $connection = Database::getConnection(); |
110 $connection->insert('file_usage') | |
108 ->fields([ | 111 ->fields([ |
109 'fid' => $file->id(), | 112 'fid' => $file->id(), |
110 'module' => 'testing', | 113 'module' => 'testing', |
111 'type' => 'bar', | 114 'type' => 'bar', |
112 'id' => 2, | 115 'id' => 2, |
114 ]) | 117 ]) |
115 ->execute(); | 118 ->execute(); |
116 | 119 |
117 // Normal decrement. | 120 // Normal decrement. |
118 $file_usage->delete($file, 'testing', 'bar', 2); | 121 $file_usage->delete($file, 'testing', 'bar', 2); |
119 $count = db_select('file_usage', 'f') | 122 $count = $connection->select('file_usage', 'f') |
120 ->fields('f', ['count']) | 123 ->fields('f', ['count']) |
121 ->condition('f.fid', $file->id()) | 124 ->condition('f.fid', $file->id()) |
122 ->execute() | 125 ->execute() |
123 ->fetchField(); | 126 ->fetchField(); |
124 $this->assertEqual(2, $count, 'The count was decremented correctly.'); | 127 $this->assertEqual(2, $count, 'The count was decremented correctly.'); |
125 | 128 |
126 // Multiple decrement and removal. | 129 // Multiple decrement and removal. |
127 $file_usage->delete($file, 'testing', 'bar', 2, 2); | 130 $file_usage->delete($file, 'testing', 'bar', 2, 2); |
128 $count = db_select('file_usage', 'f') | 131 $count = $connection->select('file_usage', 'f') |
129 ->fields('f', ['count']) | 132 ->fields('f', ['count']) |
130 ->condition('f.fid', $file->id()) | 133 ->condition('f.fid', $file->id()) |
131 ->execute() | 134 ->execute() |
132 ->fetchField(); | 135 ->fetchField(); |
133 $this->assertIdentical(FALSE, $count, 'The count was removed entirely when empty.'); | 136 $this->assertIdentical(FALSE, $count, 'The count was removed entirely when empty.'); |
134 | 137 |
135 // Non-existent decrement. | 138 // Non-existent decrement. |
136 $file_usage->delete($file, 'testing', 'bar', 2); | 139 $file_usage->delete($file, 'testing', 'bar', 2); |
137 $count = db_select('file_usage', 'f') | 140 $count = $connection->select('file_usage', 'f') |
138 ->fields('f', ['count']) | 141 ->fields('f', ['count']) |
139 ->condition('f.fid', $file->id()) | 142 ->condition('f.fid', $file->id()) |
140 ->execute() | 143 ->execute() |
141 ->fetchField(); | 144 ->fetchField(); |
142 $this->assertIdentical(FALSE, $count, 'Decrementing non-exist record complete.'); | 145 $this->assertIdentical(FALSE, $count, 'Decrementing non-exist record complete.'); |
150 * timestamp. | 153 * timestamp. |
151 */ | 154 */ |
152 public function createTempFiles() { | 155 public function createTempFiles() { |
153 // Temporary file that is old. | 156 // Temporary file that is old. |
154 $temp_old = file_save_data(''); | 157 $temp_old = file_save_data(''); |
155 db_update('file_managed') | 158 $connection = Database::getConnection(); |
159 $connection->update('file_managed') | |
156 ->fields([ | 160 ->fields([ |
157 'status' => 0, | 161 'status' => 0, |
158 'changed' => REQUEST_TIME - $this->config('system.file')->get('temporary_maximum_age') - 1, | 162 'changed' => REQUEST_TIME - $this->config('system.file')->get('temporary_maximum_age') - 1, |
159 ]) | 163 ]) |
160 ->condition('fid', $temp_old->id()) | 164 ->condition('fid', $temp_old->id()) |
161 ->execute(); | 165 ->execute(); |
162 $this->assertTrue(file_exists($temp_old->getFileUri()), 'Old temp file was created correctly.'); | 166 $this->assertTrue(file_exists($temp_old->getFileUri()), 'Old temp file was created correctly.'); |
163 | 167 |
164 // Temporary file that is new. | 168 // Temporary file that is new. |
165 $temp_new = file_save_data(''); | 169 $temp_new = file_save_data(''); |
166 db_update('file_managed') | 170 $connection->update('file_managed') |
167 ->fields(['status' => 0]) | 171 ->fields(['status' => 0]) |
168 ->condition('fid', $temp_new->id()) | 172 ->condition('fid', $temp_new->id()) |
169 ->execute(); | 173 ->execute(); |
170 $this->assertTrue(file_exists($temp_new->getFileUri()), 'New temp file was created correctly.'); | 174 $this->assertTrue(file_exists($temp_new->getFileUri()), 'New temp file was created correctly.'); |
171 | 175 |
172 // Permanent file that is old. | 176 // Permanent file that is old. |
173 $perm_old = file_save_data(''); | 177 $perm_old = file_save_data(''); |
174 db_update('file_managed') | 178 $connection->update('file_managed') |
175 ->fields(['changed' => REQUEST_TIME - $this->config('system.file')->get('temporary_maximum_age') - 1]) | 179 ->fields(['changed' => REQUEST_TIME - $this->config('system.file')->get('temporary_maximum_age') - 1]) |
176 ->condition('fid', $temp_old->id()) | 180 ->condition('fid', $temp_old->id()) |
177 ->execute(); | 181 ->execute(); |
178 $this->assertTrue(file_exists($perm_old->getFileUri()), 'Old permanent file was created correctly.'); | 182 $this->assertTrue(file_exists($perm_old->getFileUri()), 'Old permanent file was created correctly.'); |
179 | 183 |