View source
<?php
namespace Drupal\Tests\media_entity_file_replace\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
class MediaEntityFileReplaceTest extends BrowserTestBase {
use MediaTypeCreationTrait;
protected $defaultTheme = 'stark';
public static $modules = [
'system',
'field_ui',
'media',
'media_entity_file_replace',
];
public function testModule() {
$this
->createMediaType('file', [
'id' => 'document',
'label' => 'Document',
]);
$this
->createMediaType('oembed:video', [
'id' => 'remote_video',
'label' => 'Remote Video',
]);
$user = $this
->drupalCreateUser([
'access media overview',
'administer media form display',
'view media',
'administer media',
'access content',
]);
$this
->drupalLogin($user);
$this
->drupalGet('/admin/structure/media/manage/document/form-display');
$this
->assertSession()
->fieldExists("fields[replace_file][region]");
$this
->assertSession()
->fieldValueEquals('fields[replace_file][region]', 'hidden');
$this
->drupalGet('/admin/structure/media/manage/remote_video/form-display');
$this
->assertSession()
->fieldNotExists("fields[replace_file][weight]");
$page = $this
->getSession()
->getPage();
$page
->fillField('fields[name][region]', 'content');
$page
->pressButton('Save');
$this
->drupalGet('/media/add/remote_video');
$page = $this
->getSession()
->getPage();
$this
->assertSession()
->pageTextNotContains('Replace file');
$this
->assertSession()
->fieldNotExists('files[replacement_file]');
$page
->fillField('Name', 'DrupalCon Amsterdam Keynote');
$page
->fillField('Remote video URL', 'https://www.youtube.com/watch?v=Apqd4ff0NRI');
$page
->pressButton('Save');
$this
->assertSession()
->pageTextContains('Remote video DrupalCon Amsterdam Keynote has been created.');
$page
->clickLink('DrupalCon Amsterdam Keynote');
$this
->assertSession()
->fieldExists('Remote video URL');
$this
->assertSession()
->fieldNotExists('files[replacement_file]');
$uri = 'temporary://foo.txt';
file_put_contents($uri, 'original');
$this
->drupalGet('/media/add/document');
$page = $this
->getSession()
->getPage();
$this
->assertSession()
->pageTextNotContains('Replace file');
$page
->fillField('Name', 'Foobar');
$page
->attachFileToField('File', $this->container
->get('file_system')
->realpath($uri));
$this
->assertSession()
->fieldNotExists('files[replacement_file]');
$page
->pressButton('Save');
$this
->assertSession()
->addressEquals('admin/content/media');
unlink($uri);
$originalDocument = $this
->loadMediaEntityByName('Foobar');
$this
->drupalGet("/media/{$originalDocument->id()}/edit");
$this
->assertSession()
->buttonExists('Remove');
$this
->drupalGet('/admin/structure/media/manage/document/form-display');
$page = $this
->getSession()
->getPage();
$page
->fillField('fields[replace_file][region]', 'content');
$page
->pressButton('Save');
$this
->drupalGet("/media/{$originalDocument->id()}/edit");
$this
->assertSession()
->buttonNotExists('Remove');
$this
->assertSession()
->fieldExists('files[replacement_file]');
$this
->assertSession()
->fieldExists('keep_original_filename');
$originalFile = $this
->loadFileEntity($originalDocument
->getSource()
->getSourceFieldValue($originalDocument));
$uri = 'temporary://foo.txt';
file_put_contents($uri, 'new');
$page = $this
->getSession()
->getPage();
$page
->attachFileToField('File', $this->container
->get('file_system')
->realpath($uri));
$page
->checkField('keep_original_filename');
$page
->pressButton('Save');
unlink($uri);
$updatedDocument = $this
->loadMediaEntityByName('Foobar');
$updatedFile = $this
->loadFileEntity($updatedDocument
->getSource()
->getSourceFieldValue($updatedDocument));
$this
->assertEqual($updatedFile
->id(), $originalFile
->id());
$this
->assertEqual($updatedFile
->getFileUri(), $originalFile
->getFileUri());
$this
->assertEqual($updatedFile
->getFilename(), $originalFile
->getFilename());
$this
->assertNotEqual($updatedFile
->getSize(), $originalFile
->getSize());
$this
->assertEqual(file_get_contents($updatedFile
->getFileUri()), 'new');
$originalDocument = $updatedDocument;
$originalFile = $updatedFile;
$uri = 'temporary://foo-new.txt';
file_put_contents($uri, 'foo-new');
$this
->drupalGet("/media/{$originalDocument->id()}/edit");
$page = $this
->getSession()
->getPage();
$page
->attachFileToField('File', $this->container
->get('file_system')
->realpath($uri));
$page
->uncheckField('keep_original_filename');
$page
->pressButton('Save');
unlink($uri);
$updatedDocument = $this
->loadMediaEntityByName('Foobar');
$updatedFile = $this
->loadFileEntity($updatedDocument
->getSource()
->getSourceFieldValue($updatedDocument));
$this
->assertNotEqual($updatedFile
->id(), $originalFile
->id());
$this
->assertNotEqual($updatedFile
->getFileUri(), $originalFile
->getFileUri());
$this
->assertNotEqual($updatedFile
->getFilename(), $originalFile
->getFilename());
$this
->assertNotEqual($updatedFile
->getSize(), $originalFile
->getSize());
$this
->assertNotEqual(file_get_contents($updatedFile
->getFileUri()), file_get_contents($originalFile
->getFileUri()));
$this
->assertEqual(file_get_contents($updatedFile
->getFileUri()), 'foo-new');
$this
->assertFalse($updatedFile
->isTemporary());
$originalFile = $this
->loadFileEntity($originalFile
->id());
$this
->assertFalse($originalFile
->isTemporary());
$originalDocument = $this
->loadMediaEntityByName('Foobar');
$uri = 'temporary://foo.pdf';
file_put_contents($uri, 'pdf contents');
$this
->drupalGet("/media/{$originalDocument->id()}/edit");
$page = $this
->getSession()
->getPage();
$this
->assertSession()
->fieldExists('files[replacement_file]');
$page
->attachFileToField('File', $this->container
->get('file_system')
->realpath($uri));
$page
->checkField('keep_original_filename');
$page
->pressButton('Save');
$this
->assertSession()
->pageTextContains('Only files with the following extensions are allowed: txt');
$this
->assertSession()
->addressEquals("/media/{$originalDocument->id()}/edit");
$originalDocument = $this
->loadMediaEntityByName('Foobar');
$page = $this
->getSession()
->getPage();
$page
->attachFileToField('File', $this->container
->get('file_system')
->realpath($uri));
$page
->uncheckField('keep_original_filename');
$page
->pressButton('Save');
$this
->assertSession()
->pageTextNotContains('Only files with the following extensions are allowed: txt');
$this
->assertSession()
->addressEquals("/admin/content/media");
$this
->assertSession()
->pageTextNotContains('foo.pdf');
unlink($uri);
$originalDocument = $this
->loadMediaEntityByName('Foobar');
$fileToDelete = $this
->loadFileEntity($originalDocument
->getSource()
->getSourceFieldValue($originalDocument));
$fileToDelete
->delete();
$this
->drupalGet("/media/{$originalDocument->id()}/edit");
$page = $this
->getSession()
->getPage();
$this
->assertSession()
->fieldNotExists('files[replacement_file]');
}
protected function loadMediaEntityByName($name) {
$mediaStorage = \Drupal::entityTypeManager()
->getStorage('media');
$mediaStorage
->resetCache();
$entities = $mediaStorage
->loadByProperties([
'name' => $name,
]);
$this
->assertNotEmpty($entities, "No media entity with name {$name} was found.");
return array_pop($entities);
}
protected function loadFileEntity($id) {
$fileStorage = \Drupal::entityTypeManager()
->getStorage('file');
$fileStorage
->resetCache();
$file = $fileStorage
->load($id);
$this
->assertNotNull($file, "No file entity with id {$id} was found.");
return $file;
}
}