View source
<?php
namespace Drupal\Tests\filefield_sources\Functional;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
class MultipleValuesTest extends FileFieldSourcesTestBase {
protected $permanentFileEntity1;
protected $permanentFileEntity2;
protected $temporaryFileEntity1;
protected $temporaryFileEntity2;
protected $temporaryFile;
public static $modules = [
'imce',
];
protected function setUp() {
parent::setUp();
$this
->setUpImce();
$this->permanentFileEntity1 = $this
->createPermanentFileEntity();
$this->permanentFileEntity2 = $this
->createPermanentFileEntity();
$this->temporaryFileEntity1 = $this
->createTemporaryFileEntity();
$this->temporaryFileEntity2 = $this
->createTemporaryFileEntity();
$path = \Drupal::config('system.file')
->get('default_scheme') . '://' . FILEFIELD_SOURCE_ATTACH_DEFAULT_PATH . '/';
$this->temporaryFile = $this
->createTemporaryFile($path);
$this
->drupalPostForm('admin/structure/types/manage/' . $this->typeName . '/fields/node.' . $this->typeName . '.' . $this->fieldName . '/storage', [
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
], t('Save field settings'));
$this
->enableSources([
'upload' => TRUE,
'remote' => TRUE,
'clipboard' => TRUE,
'reference' => TRUE,
'attach' => TRUE,
'imce' => TRUE,
]);
}
public function testUploadThenRemoveFiles() {
$this
->uploadFiles();
$this
->removeFile('INSTALL.txt', 0);
$this
->removeFile($this->permanentFileEntity1
->getFilename(), 0);
$this
->removeFile($this->temporaryFile->filename, 0);
$this
->removeFile($this->temporaryFileEntity2
->getFilename(), 0);
$this
->removeFile($this->temporaryFileEntity1
->getFilename(), 0);
$this
->removeFile($this->permanentFileEntity2
->getFilename(), 0);
$this
->assertNoFieldByXPath('//input[@type="submit"]', t('Remove'), 'All files have been removed.');
}
public function testUploadFilesThenSaveNode() {
$this
->uploadFiles();
$this
->drupalPostForm(NULL, [
'title[0][value]' => $this
->randomMachineName(),
], t('Save'));
$this
->assertLink('INSTALL.txt');
$this
->assertLink($this->permanentFileEntity1
->getFilename());
$this
->assertLink($this->temporaryFile->filename);
$this
->assertLink($this->temporaryFileEntity2
->getFilename());
$this
->assertLink($this->temporaryFileEntity1
->getFilename());
$this
->assertLink($this->permanentFileEntity2
->getFilename());
}
protected function uploadFiles() {
$uploaded_files = 0;
$this
->assertNoFieldByXPath('//input[@type="submit"]', t('Remove'), 'There are no file have been uploaded.');
$this
->uploadFileByRemoteSource($GLOBALS['base_url'] . '/core/INSTALL.txt', 'INSTALL.txt', $uploaded_files);
$uploaded_files++;
$this
->uploadFileByReferenceSource($this->permanentFileEntity1
->id(), $this->permanentFileEntity1
->getFilename(), $uploaded_files);
$uploaded_files++;
$this
->uploadFileByAttachSource($this->temporaryFile->uri, $this->temporaryFile->filename, $uploaded_files);
$uploaded_files++;
$this
->uploadFileByUploadSource($this->temporaryFileEntity2
->getFileUri(), $this->temporaryFileEntity2
->getFilename(), $uploaded_files, TRUE);
$uploaded_files++;
$this
->uploadFileByClipboardSource($this->temporaryFileEntity1
->getFileUri(), $this->temporaryFileEntity1
->getFileName(), $uploaded_files);
$uploaded_files++;
$this
->uploadFileByImceSource($this->permanentFileEntity2
->getFileUri(), $this->permanentFileEntity2
->getFileName(), $uploaded_files);
$uploaded_files++;
$remove_buttons = $this
->xpath('//input[@type="submit" and @value="' . t('Remove') . '"]');
$this
->assertEqual(count($remove_buttons), $uploaded_files, "There are {$uploaded_files} files have been uploaded.");
return $uploaded_files;
}
}