summaryrefslogtreecommitdiffstats
path: root/core/modules/media/tests/src/FunctionalJavascript/MediaTypeCreationTest.php
blob: bd263b5576e559267fb99ba3bac3861a8c3cce54 (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php

namespace Drupal\Tests\media\FunctionalJavascript;

/**
 * Tests the media type creation.
 *
 * @group media
 */
class MediaTypeCreationTest extends MediaJavascriptTestBase {

  /**
   * Tests the media type creation form.
   */
  public function testMediaTypeCreationFormWithDefaultField() {
    $label = 'Type with Default Field';
    $mediaTypeMachineName = str_replace(' ', '_', strtolower($label));

    $this->drupalGet('admin/structure/media/add');
    $page = $this->getSession()->getPage();

    // Fill in a label to the media type.
    $page->fillField('label', $label);
    // Wait for machine name generation. Default: waitUntilVisible(), does not
    // work properly.
    $this->getSession()
      ->wait(5000, "jQuery('.machine-name-value').text() === '{$mediaTypeMachineName}'");

    // Select the media source used by our media type.
    $this->assertSession()->fieldExists('Media source');
    $this->assertSession()->optionExists('Media source', 'test');
    $page->selectFieldOption('Media source', 'test');
    $this->assertSession()->assertWaitOnAjaxRequest();

    $page->pressButton('Save');

    // Check whether the source field was correctly created.
    $this->drupalGet("admin/structure/media/manage/{$mediaTypeMachineName}/fields");

    // Check 2nd column of first data row, to be machine name for field name.
    $this->assertSession()
      ->elementContains('xpath', '(//table[@id="field-overview"]//tr)[2]//td[2]', 'field_media_test');
    // Check 3rd column of first data row, to be correct field type.
    $this->assertSession()
      ->elementTextContains('xpath', '(//table[@id="field-overview"]//tr)[2]//td[3]', 'Text (plain)');

    // Check that the source field is correctly assigned to media type.
    $this->drupalGet("admin/structure/media/manage/{$mediaTypeMachineName}");

    $this->assertSession()->pageTextContains('Test source field is used to store the essential information about the media item.');
  }

  /**
   * Test creation of media type, reusing an existing source field.
   */
  public function testMediaTypeCreationReuseSourceField() {
    // Create a new media type, which should create a new field we can reuse.
    $this->drupalGet('/admin/structure/media/add');
    $page = $this->getSession()->getPage();
    $page->fillField('label', 'Pastafazoul');
    $this->getSession()
      ->wait(5000, "jQuery('.machine-name-value').text() === 'pastafazoul'");
    $page->selectFieldOption('Media source', 'test');
    $this->assertSession()->assertWaitOnAjaxRequest();
    $page->pressButton('Save');

    $label = 'Type reusing Default Field';
    $mediaTypeMachineName = str_replace(' ', '_', strtolower($label));

    $this->drupalGet('admin/structure/media/add');
    $page = $this->getSession()->getPage();

    // Fill in a label to the media type.
    $page->fillField('label', $label);

    // Wait for machine name generation. Default: waitUntilVisible(), does not
    // work properly.
    $this->getSession()
      ->wait(5000, "jQuery('.machine-name-value').text() === '{$mediaTypeMachineName}'");

    // Select the media source used by our media type.
    $this->assertSession()->fieldExists('Media source');
    $this->assertSession()->optionExists('Media source', 'test');
    $page->selectFieldOption('Media source', 'test');
    $this->assertSession()->assertWaitOnAjaxRequest();
    // Select the existing field for re-use.
    $page->selectFieldOption('source_configuration[source_field]', 'field_media_test');
    $page->pressButton('Save');

    // Check that no new fields were created.
    $this->drupalGet("admin/structure/media/manage/{$mediaTypeMachineName}/fields");
    // The reused field should be present...
    $this->assertSession()->pageTextContains('field_media_test');
    // ...not a new, unique one.
    $this->assertSession()->pageTextNotContains('field_media_test_1');
  }

}