View source
<?php
namespace Drupal\Tests\recipe\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\BrowserTestBase;
class RecipeTotalTimeConfigTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
protected static $modules = [
'recipe',
];
protected $entityTypeManager;
protected $adminUser;
public function setUp() : void {
parent::setUp();
$this->entityTypeManager = $this->container
->get('entity_type.manager');
$permissions = [
'create recipe content',
'edit any recipe content',
'administer site configuration',
];
$this->adminUser = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($this->adminUser);
}
public function testIntegerFieldTotalTimeConfig() {
$title = $this
->randomMachineName(16);
$preptime = 60;
$cooktime = 135;
$new_field_value = 20;
$edit = [
'title[0][value]' => $title,
'recipe_prep_time[0][value]' => $preptime,
'recipe_cook_time[0][value]' => $cooktime,
];
$this
->drupalPostForm('node/add/recipe', $edit, 'Save');
$this
->assertSession()
->pageTextContains(new FormattableMarkup('Recipe @title has been created.', [
'@title' => $title,
]));
$this
->assertSession()
->pageTextContains('3 hours, 15 minutes');
$field_name = strtolower($this
->randomMachineName());
$field_settings = [
'min' => 0,
];
$this
->createIntegerField($field_name, 'node', 'recipe', [], $field_settings);
$edit = [
$field_name . '[0][value]' => $new_field_value,
];
$this
->drupalPostForm('node/1/edit', $edit, 'Save');
$this
->drupalGet('node/1');
$this
->assertSession()
->pageTextContains('20 minutes');
$this
->assertSession()
->pageTextContains('3 hours, 15 minutes');
$this
->updateFieldThirdPartySetting($field_name, 'node', 'recipe', 'total_time', 1);
$this
->drupalGet('node/1');
$this
->assertSession()
->pageTextContains('3 hours, 35 minutes');
$this
->updateFieldThirdPartySetting('recipe_cook_time', 'node', 'recipe', 'total_time', 0);
$this
->drupalGet('node/1');
$this
->assertSession()
->pageTextContains('1 hour, 20 minutes');
}
protected function createIntegerField($name, $entity_type, $bundle, array $storage_settings = [], array $field_settings = [], array $widget_settings = [], array $display_settings = []) {
$field_storage = $this->entityTypeManager
->getStorage('field_storage_config')
->create([
'entity_type' => $entity_type,
'field_name' => $name,
'type' => 'integer',
'settings' => $storage_settings,
'cardinality' => !empty($storage_settings['cardinality']) ? $storage_settings['cardinality'] : 1,
]);
$field_storage
->save();
$this
->attachIntegerField($name, $entity_type, $bundle, $field_settings, $widget_settings, $display_settings);
return $field_storage;
}
protected function attachIntegerField($name, $entity_type, $bundle, array $field_settings = [], array $widget_settings = [], array $display_settings = []) {
$field = [
'field_name' => $name,
'label' => $name,
'entity_type' => $entity_type,
'bundle' => $bundle,
'required' => !empty($field_settings['required']),
'settings' => $field_settings,
];
$this->entityTypeManager
->getStorage('field_config')
->create($field)
->save();
$form_display = $this->entityTypeManager
->getStorage('entity_form_display')
->load($entity_type . '.' . $bundle . '.default');
$form_display
->setComponent($name, [
'type' => 'number',
'settings' => $widget_settings,
])
->save();
$view_display = $this->entityTypeManager
->getStorage('entity_view_display')
->load($entity_type . '.' . $bundle . '.default');
$view_display
->setComponent($name, [
'label' => 'hidden',
'type' => 'recipe_duration',
'settings' => $display_settings,
])
->save();
}
protected function updateFieldThirdPartySetting($name, $entity_type, $bundle, $setting_name, $value) {
$field = FieldConfig::loadByName($entity_type, $bundle, $name);
$field
->setThirdPartySetting('recipe', $setting_name, $value);
$field
->save();
}
}