You are here

public function RecipeTotalTimeConfigTest::testIntegerFieldTotalTimeConfig in Recipe 8.2

Tests the integer field third-party settings.

File

tests/src/Functional/RecipeTotalTimeConfigTest.php, line 60

Class

RecipeTotalTimeConfigTest
Tests the functionality of the Recipe Total Time third-party settings.

Namespace

Drupal\Tests\recipe\Functional

Code

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,
  ];

  // Post the values to the node form.
  $this
    ->drupalPostForm('node/add/recipe', $edit, 'Save');
  $this
    ->assertSession()
    ->pageTextContains(new FormattableMarkup('Recipe @title has been created.', [
    '@title' => $title,
  ]));

  // Check for the total time.
  $this
    ->assertSession()
    ->pageTextContains('3 hours, 15 minutes');

  // Add another integer field, but don't configure it as a total time field.
  $field_name = strtolower($this
    ->randomMachineName());
  $field_settings = [
    'min' => 0,
  ];
  $this
    ->createIntegerField($field_name, 'node', 'recipe', [], $field_settings);

  // Add a value for the new field to the existing node.
  $edit = [
    $field_name . '[0][value]' => $new_field_value,
  ];
  $this
    ->drupalPostForm('node/1/edit', $edit, 'Save');

  // Check for the new field's value.
  $this
    ->drupalGet('node/1');
  $this
    ->assertSession()
    ->pageTextContains('20 minutes');
  $this
    ->assertSession()
    ->pageTextContains('3 hours, 15 minutes');

  // Configure the new field as a total time field.
  $this
    ->updateFieldThirdPartySetting($field_name, 'node', 'recipe', 'total_time', 1);

  // Check for the updated total time value.
  $this
    ->drupalGet('node/1');
  $this
    ->assertSession()
    ->pageTextContains('3 hours, 35 minutes');

  // De-configure the recipe_cook_time field as a total time field.
  $this
    ->updateFieldThirdPartySetting('recipe_cook_time', 'node', 'recipe', 'total_time', 0);

  // Check that fields can be de-configured as total time fields.
  $this
    ->drupalGet('node/1');
  $this
    ->assertSession()
    ->pageTextContains('1 hour, 20 minutes');
}