a1de95c01bf78f626941296b4ea5760589101f09
[yaffs-website] / web / core / modules / quickedit / tests / src / FunctionalJavascript / QuickEditFileTest.php
1 <?php
2
3 namespace Drupal\Tests\quickedit\FunctionalJavascript;
4
5 use Drupal\file\Entity\File;
6 use Drupal\node\Entity\Node;
7 use Drupal\Tests\file\Functional\FileFieldCreationTrait;
8 use Drupal\Tests\TestFileCreationTrait;
9
10 /**
11  * @group quickedit
12  */
13 class QuickEditFileTest extends QuickEditJavascriptTestBase {
14
15   use FileFieldCreationTrait;
16   use TestFileCreationTrait;
17
18   /**
19    * {@inheritdoc}
20    */
21   public static $modules = [
22     'node',
23     'file',
24   ];
25
26   /**
27    * {@inheritdoc}
28    */
29   protected function setUp() {
30     parent::setUp();
31
32     // Create the Article node type.
33     $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
34
35     // Add file field to Article node type.
36     $this->createFileField('field_file', 'node', 'article', ['file_extensions' => 'txt']);
37
38     // Log in as a content author who can use Quick Edit and edit Articles.
39     $user = $this->drupalCreateUser([
40       'access contextual links',
41       'access toolbar',
42       'access in-place editing',
43       'access content',
44       'create article content',
45       'edit any article content',
46     ]);
47     $this->drupalLogin($user);
48   }
49
50   /**
51    * Tests if a file can be in-place removed with Quick Edit.
52    */
53   public function testRemove() {
54     $assert_session = $this->assertSession();
55
56     // Create test file.
57     $this->generateFile('test', 64, 10, 'text');
58     $file = File::create([
59       'uri' => 'public://test.txt',
60       'filename' => 'test.txt',
61     ]);
62     $file->setPermanent();
63     $file->save();
64
65     // Create test node.
66     $node = $this->drupalCreateNode([
67       'type' => 'article',
68       'title' => t('My Test Node'),
69       'field_file' => [
70         'target_id' => $file->id(),
71       ],
72     ]);
73
74     $this->drupalGet($node->toUrl()->toString());
75
76     // Start Quick Edit.
77     $this->awaitQuickEditForEntity('node', 1);
78     $this->startQuickEditViaToolbar('node', 1, 0);
79
80     // Click the file field.
81     $assert_session->waitForElementVisible('css', '[data-quickedit-field-id="node/1/field_file/en/full"]');
82     $this->click('[data-quickedit-field-id="node/1/field_file/en/full"]');
83     $assert_session->waitForElement('css', '.quickedit-toolbar-field div[id*="file"]');
84
85     // Remove the file.
86     $remove = $assert_session->waitForButton('Remove');
87     $remove->click();
88     // Wait for remove.
89     $assert_session->waitForElement('css', 'input[name="files[field_file_0]"]');
90     $this->saveQuickEdit();
91     // Wait for save.
92     $this->assertJsCondition("Drupal.quickedit.collections.entities.get('node/1[0]').get('state') === 'closed'");
93
94     // Assert file is removed from node.
95     $assert_session->pageTextNotContains('test.txt');
96     $node = Node::load($node->id());
97     $this->assertEmpty($node->get('field_file')->getValue());
98   }
99
100 }