View source
<?php
namespace Drupal\field_defaults\Tests;
use Drupal\Tests\BrowserTestBase;
class FieldDefaultsTestBase extends BrowserTestBase {
protected $administratorAccount;
protected $fieldName;
public static $modules = [
'block',
'node',
'field_ui',
'field_defaults',
];
protected function setUp() {
parent::setUp();
$this
->setupMenus();
$this
->drupalCreateContentType([
'type' => 'page',
]);
$permissions = [
'administer nodes',
'administer content types',
'administer node fields',
'edit any page content',
'administer field defaults',
];
$this->administratorAccount = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($this->administratorAccount);
for ($i = 0; $i < 20; $i++) {
$this
->drupalCreateNode();
}
}
protected function setupMenus() {
$this
->drupalPlaceBlock('system_menu_block:tools', [
'region' => 'primary_menu',
]);
$this
->drupalPlaceBlock('local_tasks_block', [
'region' => 'secondary_menu',
]);
$this
->drupalPlaceBlock('local_actions_block', [
'region' => 'content',
]);
$this
->drupalPlaceBlock('page_title_block', [
'region' => 'content',
]);
}
protected function createField($type = 'boolean', $cardinality = '1', $contentType = 'page') {
$this
->drupalGet('admin/structure/types/manage/' . $contentType . '/fields');
$this
->clickLink('Add field');
$field_name = strtolower($this
->randomMachineName(10));
$edit = [
'new_storage_type' => $type,
'field_name' => $field_name,
'label' => $field_name,
];
$this
->drupalPostForm(NULL, $edit, t('Save and continue'));
$edit = [
'cardinality' => 'number',
'cardinality_number' => (string) $cardinality,
];
if (-1 == $cardinality) {
$edit = [
'cardinality' => '-1',
'cardinality_number' => '1',
];
}
$this
->drupalPostForm(NULL, $edit, t('Save field settings'));
$this
->assertText(t('Updated field @name field settings.', [
'@name' => $field_name,
]));
$this
->drupalPostForm(NULL, [], t('Save settings'));
$this
->assertText(t('Saved @name configuration.', [
'@name' => $field_name,
]));
return $field_name;
}
protected function setDefaultValues($fieldName, $field_type = 'boolean', $values = [], $contentType = 'page') {
$this
->drupalGet('admin/structure/types/manage/' . $contentType . '/fields/node.' . $contentType . '.field_' . $fieldName);
$field_setup = $this
->setupFieldByType($field_type);
$edit = [
'default_value_input[field_' . $fieldName . ']' . $field_setup['structure'] => $field_setup['value'],
'default_value_input[field_defaults][update_defaults]' => TRUE,
];
$this
->drupalPostForm(NULL, $edit, t('Save settings'));
$this
->assertNoRaw('&nbsp;', 'Initial progress message is not double escaped.');
$this->maximumMetaRefreshCount = 1;
$this
->assertRaw('<li class="messages__item">Default values were updated for 20 entities.</li>');
}
protected function setupFieldByType($type) {
switch ($type) {
case 'string':
$structure = '[0][value]';
$value = 'field default';
break;
default:
$structure = '[value]';
$value = TRUE;
}
return [
'structure' => $structure,
'value' => $value,
];
}
}