97052ee230fdc5a37f1cac6e50a1fa08b5558df6
[yaffs-website] / web / modules / contrib / filefield_sources / src / Tests / MultipleValuesTest.php
1 <?php
2
3 /**
4  * @file
5  * Definition of Drupal\filefield_sources\Tests\MultipleValuesTest.
6  */
7
8 namespace Drupal\filefield_sources\Tests;
9
10 use Drupal\Core\Field\FieldStorageDefinitionInterface;
11
12 /**
13  * Tests multiple sources on multiple values field.
14  *
15  * @group filefield_sources
16  */
17 class MultipleValuesTest extends FileFieldSourcesTestBase {
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = array('imce');
25
26   /**
27    * Sets up for multiple values test case.
28    */
29   protected function setUp() {
30     parent::setUp();
31     $this->setUpImce();
32
33     // Create test files.
34     $this->permanent_file_entity_1 = $this->createPermanentFileEntity();
35     $this->permanent_file_entity_2 = $this->createPermanentFileEntity();
36     $this->temporary_file_entity_1 = $this->createTemporaryFileEntity();
37     $this->temporary_file_entity_2 = $this->createTemporaryFileEntity();
38
39     $path = file_default_scheme() . '://' . FILEFIELD_SOURCE_ATTACH_DEFAULT_PATH . '/';
40     $this->temporary_file = $this->createTemporaryFile($path);
41
42     // Change allowed number of values.
43     $this->drupalPostForm('admin/structure/types/manage/' . $this->typeName . '/fields/node.' . $this->typeName . '.' . $this->fieldName . '/storage', array('cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED), t('Save field settings'));
44
45     $this->enableSources(array(
46       'upload' => TRUE,
47       'remote' => TRUE,
48       'clipboard' => TRUE,
49       'reference' => TRUE,
50       'attach' => TRUE,
51       'imce' => TRUE,
52     ));
53   }
54
55   /**
56    * Tests uploading then removing files.
57    */
58   public function testUploadThenRemoveFiles() {
59     $this->uploadFiles();
60
61     // Remove all uploaded files.
62     $this->removeFile($this->temporary_file_entity_2->getFilename(), 4);
63     $this->removeFile('INSTALL.txt', 0);
64     $this->removeFile($this->temporary_file_entity_1->getFilename(), 1);
65     $this->removeFile($this->temporary_file->filename, 1);
66     $this->removeFile($this->permanent_file_entity_1->getFilename(), 0);
67     $this->removeFile($this->permanent_file_entity_2->getFilename(), 0);
68
69     // Ensure all files have been removed.
70     $this->assertNoFieldByXPath('//input[@type="submit"]', t('Remove'), 'All files have been removed.');
71   }
72
73   /**
74    * Tests uploading files and saving node.
75    */
76   public function testUploadFilesThenSaveNode() {
77     $this->uploadFiles();
78
79     $this->drupalPostForm(NULL, array('title[0][value]' => $this->randomMachineName()), t('Save and publish'));
80
81     // Ensure all files are saved to node.
82     $this->assertLink('INSTALL.txt');
83     $this->assertLink($this->permanent_file_entity_1->getFilename());
84     $this->assertLink($this->permanent_file_entity_2->getFilename());
85     $this->assertLink($this->temporary_file_entity_1->getFilename());
86     $this->assertLink($this->temporary_file_entity_2->getFilename());
87     $this->assertLink($this->temporary_file->filename);
88   }
89
90   /**
91    * Upload files.
92    *
93    * @return int
94    *   Number of files uploaded.
95    */
96   protected function uploadFiles() {
97     $uploaded_files = 0;
98
99     // Ensure no files has been uploaded.
100     $this->assertNoFieldByXPath('//input[@type="submit"]', t('Remove'), 'There are no file have been uploaded.');
101
102     // Upload a file by 'Remote' source.
103     $this->uploadFileByRemoteSource($GLOBALS['base_url'] . '/core/INSTALL.txt', 'INSTALL.txt', $uploaded_files);
104     $uploaded_files++;
105
106     // Upload a file by 'Reference' source.
107     $this->uploadFileByReferenceSource($this->permanent_file_entity_1->id(), $this->permanent_file_entity_1->getFilename(), $uploaded_files);
108     $uploaded_files++;
109
110     // Upload a file by 'Clipboard' source.
111     $this->uploadFileByClipboardSource($this->temporary_file_entity_1->getFileUri(), $this->temporary_file_entity_1->getFileName(), $uploaded_files);
112     $uploaded_files++;
113
114     // Upload a file by 'Attach' source.
115     $this->uploadFileByAttachSource($this->temporary_file->uri, $this->temporary_file->filename, $uploaded_files);
116     $uploaded_files++;
117
118     // Upload a file by 'Upload' source.
119     $this->uploadFileByUploadSource($this->temporary_file_entity_2->getFileUri(), $this->temporary_file_entity_2->getFilename(), $uploaded_files, TRUE);
120     $uploaded_files++;
121
122     // Upload a file by 'Imce' source.
123     $this->uploadFileByImceSource($this->permanent_file_entity_2->getFileUri(), $this->permanent_file_entity_2->getFileName(), $uploaded_files);
124     $uploaded_files++;
125
126     // Ensure files have been uploaded.
127     $remove_buttons = $this->xpath('//input[@type="submit" and @value="' . t('Remove') . '"]');
128     $this->assertEqual(count($remove_buttons), $uploaded_files, "There are $uploaded_files files have been uploaded.");
129
130     return $uploaded_files;
131   }
132
133 }