Chris@0: drupalLogin($this->drupalCreateUser([
Chris@0: 'view test entity',
Chris@0: 'administer entity_test content',
Chris@0: 'administer entity_test form display',
Chris@0: 'administer entity_test fields',
Chris@0: ]));
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Tests boolean field.
Chris@0: */
Chris@0: public function testBooleanField() {
Chris@0: $on = $this->randomMachineName();
Chris@0: $off = $this->randomMachineName();
Chris@0: $label = $this->randomMachineName();
Chris@0:
Chris@0: // Create a field with settings to validate.
Chris@0: $field_name = Unicode::strtolower($this->randomMachineName());
Chris@0: $this->fieldStorage = FieldStorageConfig::create([
Chris@0: 'field_name' => $field_name,
Chris@0: 'entity_type' => 'entity_test',
Chris@0: 'type' => 'boolean',
Chris@0: ]);
Chris@0: $this->fieldStorage->save();
Chris@0: $this->field = FieldConfig::create([
Chris@0: 'field_name' => $field_name,
Chris@0: 'entity_type' => 'entity_test',
Chris@0: 'bundle' => 'entity_test',
Chris@0: 'label' => $label,
Chris@0: 'required' => TRUE,
Chris@0: 'settings' => [
Chris@0: 'on_label' => $on,
Chris@0: 'off_label' => $off,
Chris@0: ],
Chris@0: ]);
Chris@0: $this->field->save();
Chris@0:
Chris@0: // Create a form display for the default form mode.
Chris@0: entity_get_form_display('entity_test', 'entity_test', 'default')
Chris@0: ->setComponent($field_name, [
Chris@0: 'type' => 'boolean_checkbox',
Chris@0: ])
Chris@0: ->save();
Chris@0: // Create a display for the full view mode.
Chris@0: entity_get_display('entity_test', 'entity_test', 'full')
Chris@0: ->setComponent($field_name, [
Chris@0: 'type' => 'boolean',
Chris@0: ])
Chris@0: ->save();
Chris@0:
Chris@0: // Display creation form.
Chris@0: $this->drupalGet('entity_test/add');
Chris@0: $this->assertFieldByName("{$field_name}[value]", '', 'Widget found.');
Chris@0: $this->assertText($this->field->label(), 'Uses field label by default.');
Chris@0: $this->assertNoRaw($on, 'Does not use the "On" label.');
Chris@0:
Chris@0: // Submit and ensure it is accepted.
Chris@0: $edit = [
Chris@0: "{$field_name}[value]" => 1,
Chris@0: ];
Chris@0: $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0: preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
Chris@0: $id = $match[1];
Chris@0: $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
Chris@0:
Chris@0: // Verify that boolean value is displayed.
Chris@0: $entity = EntityTest::load($id);
Chris@0: $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full');
Chris@0: $content = $display->build($entity);
Chris@0: $this->setRawContent(\Drupal::service('renderer')->renderRoot($content));
Chris@0: $this->assertRaw('
' . $on . '
');
Chris@0:
Chris@0: // Test with "On" label option.
Chris@0: entity_get_form_display('entity_test', 'entity_test', 'default')
Chris@0: ->setComponent($field_name, [
Chris@0: 'type' => 'boolean_checkbox',
Chris@0: 'settings' => [
Chris@0: 'display_label' => FALSE,
Chris@0: ]
Chris@0: ])
Chris@0: ->save();
Chris@0:
Chris@0: $this->drupalGet('entity_test/add');
Chris@0: $this->assertFieldByName("{$field_name}[value]", '', 'Widget found.');
Chris@0: $this->assertRaw($on);
Chris@0: $this->assertNoText($this->field->label());
Chris@0:
Chris@0: // Test if we can change the on label.
Chris@0: $on = $this->randomMachineName();
Chris@0: $edit = [
Chris@0: 'settings[on_label]' => $on,
Chris@0: ];
Chris@0: $this->drupalPostForm('entity_test/structure/entity_test/fields/entity_test.entity_test.' . $field_name, $edit, t('Save settings'));
Chris@0: // Check if we see the updated labels in the creation form.
Chris@0: $this->drupalGet('entity_test/add');
Chris@0: $this->assertRaw($on);
Chris@0:
Chris@0: // Go to the form display page and check if the default settings works as
Chris@0: // expected.
Chris@0: $fieldEditUrl = 'entity_test/structure/entity_test/form-display';
Chris@0: $this->drupalGet($fieldEditUrl);
Chris@0:
Chris@0: // Click on the widget settings button to open the widget settings form.
Chris@0: $this->drupalPostAjaxForm(NULL, [], $field_name . "_settings_edit");
Chris@0:
Chris@0: $this->assertText(
Chris@0: 'Use field label instead of the "On label" as label',
Chris@0: t('Display setting checkbox available.')
Chris@0: );
Chris@0:
Chris@0: // Enable setting.
Chris@0: $edit = ['fields[' . $field_name . '][settings_edit_form][settings][display_label]' => 1];
Chris@0: $this->drupalPostAjaxForm(NULL, $edit, $field_name . "_plugin_settings_update");
Chris@0: $this->drupalPostForm(NULL, NULL, 'Save');
Chris@0:
Chris@0: // Go again to the form display page and check if the setting
Chris@0: // is stored and has the expected effect.
Chris@0: $this->drupalGet($fieldEditUrl);
Chris@0: $this->assertText('Use field label: Yes', 'Checking the display settings checkbox updated the value.');
Chris@0:
Chris@0: $this->drupalPostAjaxForm(NULL, [], $field_name . "_settings_edit");
Chris@0: $this->assertText(
Chris@0: 'Use field label instead of the "On label" as label',
Chris@0: t('Display setting checkbox is available')
Chris@0: );
Chris@0: $this->assertFieldByXPath(
Chris@0: '*//input[starts-with(@id, "edit-fields-' . $field_name . '-settings-edit-form-settings-display-label") and @value="1"]',
Chris@0: TRUE,
Chris@0: t('Display label changes label of the checkbox')
Chris@0: );
Chris@0:
Chris@0: // Test the boolean field settings.
Chris@0: $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.' . $field_name);
Chris@0: $this->assertFieldById('edit-settings-on-label', $on);
Chris@0: $this->assertFieldById('edit-settings-off-label', $off);
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Test field access.
Chris@0: */
Chris@0: public function testFormAccess() {
Chris@0: $on = 'boolean_on';
Chris@0: $off = 'boolean_off';
Chris@0: $label = 'boolean_label';
Chris@0: $field_name = 'boolean_name';
Chris@0: $this->fieldStorage = FieldStorageConfig::create([
Chris@0: 'field_name' => $field_name,
Chris@0: 'entity_type' => 'entity_test',
Chris@0: 'type' => 'boolean',
Chris@0: ]);
Chris@0: $this->fieldStorage->save();
Chris@0: $this->field = FieldConfig::create([
Chris@0: 'field_name' => $field_name,
Chris@0: 'entity_type' => 'entity_test',
Chris@0: 'bundle' => 'entity_test',
Chris@0: 'label' => $label,
Chris@0: 'settings' => [
Chris@0: 'on_label' => $on,
Chris@0: 'off_label' => $off,
Chris@0: ],
Chris@0: ]);
Chris@0: $this->field->save();
Chris@0:
Chris@0: // Create a form display for the default form mode.
Chris@0: entity_get_form_display('entity_test', 'entity_test', 'default')
Chris@0: ->setComponent($field_name, [
Chris@0: 'type' => 'boolean_checkbox',
Chris@0: ])
Chris@0: ->save();
Chris@0:
Chris@0: // Create a display for the full view mode.
Chris@0: entity_get_display('entity_test', 'entity_test', 'full')
Chris@0: ->setComponent($field_name, [
Chris@0: 'type' => 'boolean',
Chris@0: ])
Chris@0: ->save();
Chris@0:
Chris@0: // Display creation form.
Chris@0: $this->drupalGet('entity_test/add');
Chris@0: $this->assertFieldByName("{$field_name}[value]");
Chris@0:
Chris@0: // Should be posted OK.
Chris@0: $this->drupalPostForm(NULL, [], t('Save'));
Chris@0: preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
Chris@0: $id = $match[1];
Chris@0: $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
Chris@0:
Chris@0: // Tell the test module to disable access to the field.
Chris@0: \Drupal::state()->set('field.test_boolean_field_access_field', $field_name);
Chris@0: $this->drupalGet('entity_test/add');
Chris@0: // Field should not be there anymore.
Chris@0: $this->assertNoFieldByName("{$field_name}[value]");
Chris@0: // Should still be able to post the form.
Chris@0: $this->drupalPostForm(NULL, [], t('Save'));
Chris@0: preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
Chris@0: $id = $match[1];
Chris@0: $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
Chris@0: }
Chris@0:
Chris@0: }