Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / entity_browser / tests / src / FunctionalJavascript / EntityBrowserViewsWidgetTest.php
1 <?php
2
3 namespace Drupal\Tests\entity_browser\FunctionalJavascript;
4
5 use Drupal\file\Entity\File;
6
7 /**
8  * Entity Browser views widget tests.
9  *
10  * @group entity_browser
11  * @see \Drupal\entity_browser\Plugin\EntityBrowser\Widget\View
12  */
13 class EntityBrowserViewsWidgetTest extends EntityBrowserJavascriptTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = [
21     'node',
22     'views',
23     'entity_browser_test',
24   ];
25
26   /**
27    * {@inheritdoc}
28    */
29   protected function setUp() {
30     parent::setUp();
31
32     $user = $this->drupalCreateUser([
33       'access test_entity_browser_file entity browser pages',
34     ]);
35     $this->drupalLogin($user);
36   }
37
38   /**
39    * Tests Entity Browser views widget.
40    */
41   public function testViewsWidget() {
42     // Create a file so that our test View isn't empty.
43     file_unmanaged_copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://example.jpg');
44     /** @var \Drupal\file\FileInterface $file */
45     $file = File::create([
46       'uri' => 'public://example.jpg',
47     ]);
48     $file->save();
49
50     // Visit a test entity browser page that defaults to using a View widget.
51     $this->drupalGet('/entity-browser/iframe/test_entity_browser_file');
52     $field = 'entity_browser_select[file:' . $file->id() . ']';
53
54     // Test exposed filters.
55     $this->assertSession()->pageTextContains('example.jpg');
56     $this->assertSession()->fieldExists($field);
57     $this->getSession()->getPage()->fillField('filename', 'llama');
58     $this->getSession()->getPage()->pressButton('Apply');
59     $this->waitForAjaxToFinish();
60     $this->assertSession()->fieldNotExists($field);
61     $this->assertSession()->pageTextNotContains('example.jpg');
62     $this->getSession()->getPage()->fillField('filename', 'example');
63     $this->getSession()->getPage()->pressButton('Apply');
64     $this->waitForAjaxToFinish();
65     $this->assertSession()->pageTextContains('example.jpg');
66     $this->assertSession()->fieldExists($field);
67
68     // Test selection.
69     $this->submitForm([
70       $field => 1,
71     ], t('Select entities'));
72     $this->assertSession()->pageTextContains($file->getFilename());
73
74     // Create another file to test bulk select form.
75     file_unmanaged_copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://example_1.jpg');
76     /** @var \Drupal\file\FileInterface $file */
77     $new_file = File::create([
78       'uri' => 'public://example_1.jpg',
79     ]);
80     $new_file->save();
81     // Visit entity browser test page again.
82     $this->drupalGet('/entity-browser/iframe/test_entity_browser_file');
83     $new_field = 'entity_browser_select[file:' . $new_file->id() . ']';
84     // Assert both checkbox fields are there.
85     $check_old = $this->assertSession()->fieldExists($field);
86     $check_new = $this->assertSession()->fieldExists($new_field);
87     // Compare value attributes of checkboxes and assert they not equal.
88     $this->assertNotEquals($check_old->getAttribute('value'), $check_new->getAttribute('value'));
89
90     $uuid = \Drupal::service('uuid')->generate();
91     \Drupal::service('entity_browser.selection_storage')->setWithExpire(
92       $uuid,
93       ['validators' => ['cardinality' => ['cardinality' => 1]]],
94       21600
95     );
96     $this->drupalGet('/entity-browser/iframe/test_entity_browser_file', ['query' => ['uuid' => $uuid]]);
97     $this->getSession()->getPage()->fillField('entity_browser_select[file:1]', TRUE);
98     $this->getSession()->getPage()->fillField('entity_browser_select[file:2]', TRUE);
99     $this->getSession()->getPage()->pressButton('Select entities');
100
101     $this->assertSession()->pageTextContains('You can not select more than 1 entity.');
102     $this->assertSession()->checkboxNotChecked('entity_browser_select[file:1]');
103     $this->assertSession()->checkboxNotChecked('entity_browser_select[file:2]');
104   }
105
106 }