Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / field / tests / src / FunctionalJavascript / FormJSAddMoreTest.php
1 <?php
2
3 namespace Drupal\Tests\field\FunctionalJavascript;
4
5 use Drupal\Core\Entity\Entity\EntityFormDisplay;
6 use Drupal\Core\Field\FieldStorageDefinitionInterface;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\field\Entity\FieldStorageConfig;
9 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
10
11 /**
12  * Tests add more behavior for a multiple value field.
13  *
14  * @group field
15  */
16 class FormJSAddMoreTest extends WebDriverTestBase {
17
18   /**
19    * {@inheritdoc}
20    */
21   public static $modules = ['field_test', 'entity_test'];
22
23   /**
24    * {@inheritdoc}
25    */
26   protected function setUp() {
27     parent::setUp();
28
29     $account = $this->drupalCreateUser([
30       'view test entity',
31       'administer entity_test content',
32     ]);
33     $this->drupalLogin($account);
34
35     $field = [
36       'field_name' => 'field_unlimited',
37       'entity_type' => 'entity_test',
38       'bundle' => 'entity_test',
39       'label' => $this->randomMachineName() . '_label',
40       'description' => '[site:name]_description',
41       'weight' => mt_rand(0, 127),
42       'settings' => [
43         'test_field_setting' => $this->randomMachineName(),
44       ],
45     ];
46
47     FieldStorageConfig::create([
48       'field_name' => 'field_unlimited',
49       'entity_type' => 'entity_test',
50       'type' => 'test_field',
51       'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
52     ])->save();
53     FieldConfig::create($field)->save();
54
55     $entity_form_display = EntityFormDisplay::load($field['entity_type'] . '.' . $field['bundle'] . '.default');
56     $entity_form_display->setComponent($field['field_name'])->save();
57   }
58
59   /**
60    * Tests the 'Add more' functionality.
61    */
62   public function testFieldFormJsAddMore() {
63     $this->drupalGet('entity_test/add');
64
65     $assert_session = $this->assertSession();
66     $page = $this->getSession()->getPage();
67     $add_more_button = $page->findButton('field_unlimited_add_more');
68
69     // First set a value on the first input field.
70     $field_0 = $page->findField('field_unlimited[0][value]');
71     $field_0->setValue('1');
72
73     // Add another item.
74     $add_more_button->click();
75     $field_1 = $assert_session->waitForField('field_unlimited[1][value]');
76     $this->assertNotEmpty($field_1, 'Successfully added another item.');
77
78     // Validate the value of the first field has not changed.
79     $this->assertEquals('1', $field_0->getValue(), 'Value for the first item has not changed.');
80
81     // Validate the value of the second item is empty.
82     $this->assertEmpty($field_1->getValue(), 'Value for the second item is currently empty.');
83
84     // Add another item.
85     $add_more_button->click();
86     $field_2 = $assert_session->waitForField('field_unlimited[2][value]');
87     $this->assertNotEmpty($field_2, 'Successfully added another item.');
88
89     // Set values for the 2nd and 3rd fields to validate dragging.
90     $field_1->setValue('2');
91     $field_2->setValue('3');
92
93     $field_weight_0 = $page->findField('field_unlimited[0][_weight]');
94     $field_weight_1 = $page->findField('field_unlimited[1][_weight]');
95     $field_weight_2 = $page->findField('field_unlimited[2][_weight]');
96
97     // Assert starting situation matches expectations.
98     $this->assertGreaterThan($field_weight_0->getValue(), $field_weight_1->getValue());
99     $this->assertGreaterThan($field_weight_1->getValue(), $field_weight_2->getValue());
100
101     // Drag the first row after the third row.
102     $dragged = $field_0->find('xpath', 'ancestor::tr[contains(@class, "draggable")]//a[@class="tabledrag-handle"]');
103     $target = $field_2->find('xpath', 'ancestor::tr[contains(@class, "draggable")]');
104     $dragged->dragTo($target);
105
106     // Assert the order of items is updated correctly after dragging.
107     $this->assertGreaterThan($field_weight_2->getValue(), $field_weight_0->getValue());
108     $this->assertGreaterThan($field_weight_1->getValue(), $field_weight_2->getValue());
109
110     // Validate the order of items conforms to the last drag action after a
111     // updating the form via the server.
112     $add_more_button->click();
113     $field_3 = $assert_session->waitForField('field_unlimited[3][value]');
114     $this->assertNotEmpty($field_3);
115     $this->assertGreaterThan($field_weight_2->getValue(), $field_weight_0->getValue());
116     $this->assertGreaterThan($field_weight_1->getValue(), $field_weight_2->getValue());
117
118     // Validate no extraneous widget is displayed.
119     $element = $page->findField('field_unlimited[4][value]');
120     $this->assertEmpty($element);
121   }
122
123 }