1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
<?php
declare(strict_types=1);
namespace Drupal\Tests\file\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
/**
* Tests the 'managed_file' element type.
*
* @group file
*/
class FileManagedFileElementTest extends WebDriverTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['node', 'file', 'file_module_test', 'field_ui'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* A user with administration permissions.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->adminUser = $this->drupalCreateUser([
'access content',
'access administration pages',
'administer site configuration',
'administer users',
'administer permissions',
'administer content types',
'administer node fields',
'administer node display',
'administer nodes',
'bypass node access',
]);
$this->drupalLogin($this->adminUser);
$this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
}
/**
* Tests the managed_file element type.
*/
public function testManagedFile(): void {
// Perform the tests with all permutations of $form['#tree'],
// $element['#extended'], and $element['#multiple'].
$filename = \Drupal::service('file_system')->tempnam('temporary://', "testManagedFile") . '.txt';
file_put_contents($filename, $this->randomString(128));
foreach ([0, 1] as $tree) {
foreach ([0, 1] as $extended) {
foreach ([0, 1] as $multiple) {
$path = 'file/test/' . $tree . '/' . $extended . '/' . $multiple;
$input_base_name = $tree ? 'nested_file' : 'file';
$file_field_name = $multiple ? 'files[' . $input_base_name . '][]' : 'files[' . $input_base_name . ']';
// Now, test the Upload and Remove buttons, with Ajax.
// Upload, then Submit.
$last_fid_prior = $this->getLastFileId();
$this->drupalGet($path);
$this->getSession()->getPage()->attachFileToField($file_field_name, $this->container->get('file_system')->realpath($filename));
$uploaded_file = $this->assertSession()->waitForElement('css', '.file--mime-text-plain');
$this->assertNotEmpty($uploaded_file);
$last_fid = $this->getLastFileId();
$this->assertGreaterThan($last_fid_prior, $last_fid, 'New file got uploaded.');
$this->submitForm([], 'Save');
// Remove, then Submit.
$remove_button_title = $multiple ? 'Remove selected' : 'Remove';
$this->drupalGet($path . '/' . $last_fid);
if ($multiple) {
$selected_checkbox = ($tree ? 'nested[file]' : 'file') . '[file_' . $last_fid . '][selected]';
$this->getSession()->getPage()->checkField($selected_checkbox);
}
$this->getSession()->getPage()->pressButton($remove_button_title);
$this->assertSession()->assertWaitOnAjaxRequest();
$this->submitForm([], 'Save');
$this->assertSession()->pageTextContains('The file ids are .');
// Upload, then Remove, then Submit.
$this->drupalGet($path);
$this->getSession()->getPage()->attachFileToField($file_field_name, $this->container->get('file_system')->realpath($filename));
$uploaded_file = $this->assertSession()->waitForElement('css', '.file--mime-text-plain');
$this->assertNotEmpty($uploaded_file);
if ($multiple) {
$selected_checkbox = ($tree ? 'nested[file]' : 'file') . '[file_' . $this->getLastFileId() . '][selected]';
$this->getSession()->getPage()->checkField($selected_checkbox);
}
$this->getSession()->getPage()->pressButton($remove_button_title);
$this->assertSession()->assertWaitOnAjaxRequest();
$this->submitForm([], 'Save');
$this->assertSession()->pageTextContains('The file ids are .');
}
}
}
}
/**
* Retrieves the fid of the last inserted file.
*/
protected function getLastFileId() {
return (int) \Drupal::entityQueryAggregate('file')
->accessCheck(FALSE)
->aggregate('fid', 'max')
->execute()[0]['fid_max'];
}
}
|