Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / file / tests / src / Functional / FileListingTest.php
1 <?php
2
3 namespace Drupal\Tests\file\Functional;
4
5 use Drupal\node\Entity\Node;
6 use Drupal\file\Entity\File;
7 use Drupal\entity_test\Entity\EntityTestConstraints;
8
9 /**
10  * Tests file listing page functionality.
11  *
12  * @group file
13  */
14 class FileListingTest extends FileFieldTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['views', 'file', 'image', 'entity_test'];
22
23   /**
24    * An authenticated user.
25    *
26    * @var \Drupal\user\UserInterface
27    */
28   protected $baseUser;
29
30   protected function setUp() {
31     parent::setUp();
32
33     // This test expects unused managed files to be marked as a temporary file.
34     $this->config('file.settings')
35       ->set('make_unused_managed_files_temporary', TRUE)
36       ->save();
37
38     $this->adminUser = $this->drupalCreateUser(['access files overview', 'bypass node access']);
39     $this->baseUser = $this->drupalCreateUser();
40     $this->createFileField('file', 'node', 'article', [], ['file_extensions' => 'txt png']);
41   }
42
43   /**
44    * Calculates total count of usages for a file.
45    *
46    * @param $usage array
47    *   Array of file usage information as returned from file_usage subsystem.
48    * @return int
49    *   Total usage count.
50    */
51   protected function sumUsages($usage) {
52     $count = 0;
53     foreach ($usage as $module) {
54       foreach ($module as $entity_type) {
55         foreach ($entity_type as $entity) {
56           $count += $entity;
57         }
58       }
59     }
60
61     return $count;
62   }
63
64   /**
65    * Tests file overview with different user permissions.
66    */
67   public function testFileListingPages() {
68     $file_usage = $this->container->get('file.usage');
69     // Users without sufficient permissions should not see file listing.
70     $this->drupalLogin($this->baseUser);
71     $this->drupalGet('admin/content/files');
72     $this->assertResponse(403);
73
74     // Log in with user with right permissions and test listing.
75     $this->drupalLogin($this->adminUser);
76
77     for ($i = 0; $i < 5; $i++) {
78       $nodes[] = $this->drupalCreateNode(['type' => 'article']);
79     }
80
81     $this->drupalGet('admin/content/files');
82     $this->assertResponse(200);
83     $this->assertText('No files available.');
84     $this->drupalGet('admin/content/files');
85     $this->assertResponse(200);
86
87     // Create a file with no usage.
88     $file = $this->createFile();
89
90     $this->drupalGet('admin/content/files/usage/' . $file->id());
91     $this->assertResponse(200);
92     $this->assertTitle(t('File usage information for @file | Drupal', ['@file' => $file->getFilename()]));
93
94     foreach ($nodes as &$node) {
95       $this->drupalGet('node/' . $node->id() . '/edit');
96       $file = $this->getTestFile('image');
97
98       $edit = [
99         'files[file_0]' => \Drupal::service('file_system')->realpath($file->getFileUri()),
100       ];
101       $this->drupalPostForm(NULL, $edit, t('Save'));
102       $node = Node::load($node->id());
103     }
104
105     $this->drupalGet('admin/content/files');
106
107     foreach ($nodes as $node) {
108       $file = File::load($node->file->target_id);
109       $this->assertText($file->getFilename());
110       $this->assertLinkByHref(file_create_url($file->getFileUri()));
111       $this->assertLinkByHref('admin/content/files/usage/' . $file->id());
112     }
113     $this->assertFalse(preg_match('/views-field-status priority-low\">\s*' . t('Temporary') . '/', $this->getSession()->getPage()->getContent()), 'All files are stored as permanent.');
114
115     // Use one file two times and check usage information.
116     $orphaned_file = $nodes[1]->file->target_id;
117     $used_file = $nodes[0]->file->target_id;
118     $nodes[1]->file->target_id = $used_file;
119     $nodes[1]->save();
120
121     $this->drupalGet('admin/content/files');
122     $file = File::load($orphaned_file);
123     $usage = $this->sumUsages($file_usage->listUsage($file));
124     $this->assertRaw('admin/content/files/usage/' . $file->id() . '">' . $usage);
125
126     $file = File::load($used_file);
127     $usage = $this->sumUsages($file_usage->listUsage($file));
128     $this->assertRaw('admin/content/files/usage/' . $file->id() . '">' . $usage);
129
130     $result = $this->xpath("//td[contains(@class, 'views-field-status') and contains(text(), :value)]", [':value' => 'Temporary']);
131     $this->assertEqual(1, count($result), 'Unused file marked as temporary.');
132
133     // Test file usage page.
134     foreach ($nodes as $node) {
135       $file = File::load($node->file->target_id);
136       $usage = $file_usage->listUsage($file);
137       $this->drupalGet('admin/content/files/usage/' . $file->id());
138       $this->assertResponse(200);
139       $this->assertText($node->getTitle(), 'Node title found on usage page.');
140       $this->assertText('node', 'Registering entity type found on usage page.');
141       $this->assertText('file', 'Registering module found on usage page.');
142       foreach ($usage as $module) {
143         foreach ($module as $entity_type) {
144           foreach ($entity_type as $entity) {
145             $this->assertText($entity, 'Usage count found on usage page.');
146           }
147         }
148       }
149       $this->assertLinkByHref('node/' . $node->id(), 0, 'Link to registering entity found on usage page.');
150     }
151   }
152
153   /**
154    * Tests file listing usage page for entities with no canonical link template.
155    */
156   public function testFileListingUsageNoLink() {
157     // Login with user with right permissions and test listing.
158     $this->drupalLogin($this->adminUser);
159
160     // Create a bundle and attach a File field to the bundle.
161     $bundle = $this->randomMachineName();
162     entity_test_create_bundle($bundle, NULL, 'entity_test_constraints');
163     $this->createFileField('field_test_file', 'entity_test_constraints', $bundle, [], ['file_extensions' => 'txt png']);
164
165     // Create file to attach to entity.
166     $file = File::create([
167       'filename' => 'druplicon.txt',
168       'uri' => 'public://druplicon.txt',
169       'filemime' => 'text/plain',
170     ]);
171     $file->setPermanent();
172     file_put_contents($file->getFileUri(), 'hello world');
173     $file->save();
174
175     // Create entity and attach the created file.
176     $entity_name = $this->randomMachineName();
177     $entity = EntityTestConstraints::create([
178       'uid' => 1,
179       'name' => $entity_name,
180       'type' => $bundle,
181       'field_test_file' => [
182         'target_id' => $file->id(),
183       ],
184     ]);
185     $entity->save();
186
187     // Create node entity and attach the created file.
188     $node = $this->drupalCreateNode(['type' => 'article', 'file' => $file]);
189     $node->save();
190
191     // Load the file usage page for the created and attached file.
192     $this->drupalGet('admin/content/files/usage/' . $file->id());
193
194     $this->assertResponse(200);
195     // Entity name should be displayed, but not linked if Entity::toUrl
196     // throws an exception
197     $this->assertText($entity_name, 'Entity name is added to file usage listing.');
198     $this->assertNoLink($entity_name, 'Linked entity name not added to file usage listing.');
199     $this->assertLink($node->getTitle());
200   }
201
202   /**
203    * Creates and saves a test file.
204    *
205    * @return \Drupal\Core\Entity\EntityInterface
206    *   A file entity.
207    */
208   protected function createFile() {
209     // Create a new file entity.
210     $file = File::create([
211       'uid' => 1,
212       'filename' => 'druplicon.txt',
213       'uri' => 'public://druplicon.txt',
214       'filemime' => 'text/plain',
215       'created' => 1,
216       'changed' => 1,
217       'status' => FILE_STATUS_PERMANENT,
218     ]);
219     file_put_contents($file->getFileUri(), 'hello world');
220
221     // Save it, inserting a new record.
222     $file->save();
223
224     return $file;
225   }
226
227 }