3 namespace Drupal\Tests\file\FunctionalJavascript;
5 use Drupal\Core\Database\Database;
6 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
9 * Tests the 'managed_file' element type.
13 class FileManagedFileElementTest extends WebDriverTestBase {
18 protected static $modules = ['node', 'file', 'file_module_test', 'field_ui'];
21 * A user with administration permissions.
23 * @var \Drupal\user\UserInterface
30 protected function setUp() {
32 $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']);
33 $this->drupalLogin($this->adminUser);
34 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
38 * Tests the managed_file element type.
40 public function testManagedFile() {
41 // Perform the tests with all permutations of $form['#tree'],
42 // $element['#extended'], and $element['#multiple'].
43 $filename = \Drupal::service('file_system')->tempnam('temporary://', "testManagedFile") . '.txt';
44 file_put_contents($filename, $this->randomString(128));
45 foreach ([0, 1] as $tree) {
46 foreach ([0, 1] as $extended) {
47 foreach ([0, 1] as $multiple) {
48 $path = 'file/test/' . $tree . '/' . $extended . '/' . $multiple;
49 $input_base_name = $tree ? 'nested_file' : 'file';
50 $file_field_name = $multiple ? 'files[' . $input_base_name . '][]' : 'files[' . $input_base_name . ']';
52 // Now, test the Upload and Remove buttons, with Ajax.
53 // Upload, then Submit.
54 $last_fid_prior = $this->getLastFileId();
55 $this->drupalGet($path);
56 $this->getSession()->getPage()->attachFileToField($file_field_name, $this->container->get('file_system')->realpath($filename));
57 $uploaded_file = $this->assertSession()->waitForElement('css', '.file--mime-text-plain');
58 $this->assertNotEmpty($uploaded_file);
59 $last_fid = $this->getLastFileId();
60 $this->assertGreaterThan($last_fid_prior, $last_fid, 'New file got uploaded.');
61 $this->drupalPostForm(NULL, [], t('Save'));
63 // Remove, then Submit.
64 $remove_button_title = $multiple ? t('Remove selected') : t('Remove');
65 $this->drupalGet($path . '/' . $last_fid);
67 $selected_checkbox = ($tree ? 'nested[file]' : 'file') . '[file_' . $last_fid . '][selected]';
68 $this->getSession()->getPage()->checkField($selected_checkbox);
70 $this->getSession()->getPage()->pressButton($remove_button_title);
71 $this->assertSession()->assertWaitOnAjaxRequest();
72 $this->drupalPostForm(NULL, [], t('Save'));
73 $this->assertSession()->responseContains(t('The file ids are %fids.', ['%fids' => '']));
75 // Upload, then Remove, then Submit.
76 $this->drupalGet($path);
77 $this->getSession()->getPage()->attachFileToField($file_field_name, $this->container->get('file_system')->realpath($filename));
78 $uploaded_file = $this->assertSession()->waitForElement('css', '.file--mime-text-plain');
79 $this->assertNotEmpty($uploaded_file);
81 $selected_checkbox = ($tree ? 'nested[file]' : 'file') . '[file_' . $this->getLastFileId() . '][selected]';
82 $this->getSession()->getPage()->checkField($selected_checkbox);
84 $this->getSession()->getPage()->pressButton($remove_button_title);
85 $this->assertSession()->assertWaitOnAjaxRequest();
87 $this->drupalPostForm(NULL, [], t('Save'));
88 $this->assertSession()->responseContains(t('The file ids are %fids.', ['%fids' => '']));
95 * Retrieves the fid of the last inserted file.
97 protected function getLastFileId() {
98 return (int) Database::getConnection()->query('SELECT MAX(fid) FROM {file_managed}')->fetchField();