Pull merge.
[yaffs-website] / web / core / modules / file / tests / src / FunctionalJavascript / FileManagedFileElementTest.php
1 <?php
2
3 namespace Drupal\Tests\file\FunctionalJavascript;
4
5 use Drupal\Core\Database\Database;
6 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
7
8 /**
9  * Tests the 'managed_file' element type.
10  *
11  * @group file
12  */
13 class FileManagedFileElementTest extends WebDriverTestBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   protected static $modules = ['node', 'file', 'file_module_test', 'field_ui'];
19
20   /**
21    * A user with administration permissions.
22    *
23    * @var \Drupal\user\UserInterface
24    */
25   protected $adminUser;
26
27   /**
28    * {@inheritdoc}
29    */
30   protected function setUp() {
31     parent::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']);
35   }
36
37   /**
38    * Tests the managed_file element type.
39    */
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 . ']';
51
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'));
62
63           // Remove, then Submit.
64           $remove_button_title = $multiple ? t('Remove selected') : t('Remove');
65           $this->drupalGet($path . '/' . $last_fid);
66           if ($multiple) {
67             $selected_checkbox = ($tree ? 'nested[file]' : 'file') . '[file_' . $last_fid . '][selected]';
68             $this->getSession()->getPage()->checkField($selected_checkbox);
69           }
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' => '']));
74
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);
80           if ($multiple) {
81             $selected_checkbox = ($tree ? 'nested[file]' : 'file') . '[file_' . $this->getLastFileId() . '][selected]';
82             $this->getSession()->getPage()->checkField($selected_checkbox);
83           }
84           $this->getSession()->getPage()->pressButton($remove_button_title);
85           $this->assertSession()->assertWaitOnAjaxRequest();
86
87           $this->drupalPostForm(NULL, [], t('Save'));
88           $this->assertSession()->responseContains(t('The file ids are %fids.', ['%fids' => '']));
89         }
90       }
91     }
92   }
93
94   /**
95    * Retrieves the fid of the last inserted file.
96    */
97   protected function getLastFileId() {
98     return (int) Database::getConnection()->query('SELECT MAX(fid) FROM {file_managed}')->fetchField();
99   }
100
101 }