MediaSourceFileTest.php 2.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php

namespace Drupal\Tests\media\Functional;

use Drupal\field\Entity\FieldConfig;
use Drupal\media\Entity\MediaType;

/**
 * Tests the file media source.
 *
 * @group media
 */
class MediaSourceFileTest extends MediaFunctionalTestBase {

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    // We need to test without any default configuration in place.
    // @TODO: Remove this as part of https://www.drupal.org/node/2883813.
    MediaType::load('file')->delete();
  }

  /**
   * Test that it's possible to change the allowed file extensions.
   */
  public function testSourceFieldSettingsEditing() {
    $session = $this->getSession();
    $page = $session->getPage();
    $assert_session = $this->assertSession();

    $media_type = $this->createMediaType([], 'file');
    $media_type_id = $media_type->id();
    $this->assertSame('txt doc docx pdf', FieldConfig::load("media.$media_type_id.field_media_file")->get('settings')['file_extensions']);

    $this->drupalGet("admin/structure/media/manage/$media_type_id/fields/media.$media_type_id.field_media_file");

    // File extension field exists.
    $assert_session->fieldExists('Allowed file extensions');

    // Add another extension.
    $page->fillField('settings[file_extensions]', 'txt, doc, docx, pdf, odt');

    $page->pressButton('Save settings');
    $this->drupalGet("admin/structure/media/manage/$media_type_id/fields/media.$media_type_id.field_media_file");

    // Verify that new extension is present.
    $assert_session->fieldValueEquals('settings[file_extensions]', 'txt, doc, docx, pdf, odt');
    $this->assertSame('txt doc docx pdf odt', FieldConfig::load("media.$media_type_id.field_media_file")->get('settings')['file_extensions']);
  }

54
55
56
57
58
59
60
61
62
63
64
  /**
   * Ensure source field deletion is not possible.
   */
  public function testPreventSourceFieldDeletion() {
    $media_type = $this->createMediaType([], 'file');
    $media_type_id = $media_type->id();

    $this->drupalGet("admin/structure/media/manage/$media_type_id/fields/media.$media_type_id.field_media_file/delete");
    $this->assertSession()->statusCodeEquals(403);
  }

65
}