View source
<?php
namespace Drupal\Tests\ingredient\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Tests\BrowserTestBase;
class IngredientFieldTest extends BrowserTestBase {
use IngredientTestTrait;
protected $defaultTheme = 'stark';
protected static $modules = [
'field_ui',
'ingredient',
'node',
];
protected $adminUser;
protected function setUp() : void {
parent::setUp();
$this
->ingredientCreateContentType();
$permissions = [
'create test_bundle content',
'access content',
'administer node fields',
'administer node display',
'add ingredient',
'view ingredient',
'administer site configuration',
];
$this->adminUser = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($this->adminUser);
}
public function testIngredientField() {
$display_settings = [
'fraction_format' => '{%d }%d/%d',
];
$this
->createIngredientField([], [], [], $display_settings);
$test_ingredients = [];
$test_ingredients[] = [
'quantity' => 1,
'unit_key' => 'tablespoon',
'name' => $this
->randomMachineName(16),
'note' => $this
->randomMachineName(16),
];
$test_ingredients[] = [
'quantity' => 2,
'unit_key' => 'tablespoon',
'name' => $this
->randomMachineName(16),
'note' => $this
->randomMachineName(16),
];
$test_ingredients[] = [
'quantity' => 0,
'unit_key' => 'tablespoon',
'name' => $this
->randomMachineName(16),
'note' => $this
->randomMachineName(16),
];
$test_ingredients[] = [
'quantity' => 1,
'unit_key' => 'tablespoon',
'name' => $this
->randomMachineName(16),
'note' => '',
];
$test_ingredients[] = [
'quantity' => 10,
'unit_key' => 'unit',
'name' => $this
->randomMachineName(16),
'note' => $this
->randomMachineName(16),
];
$test_ingredients[] = [
'quantity' => '1/4',
'unit_key' => 'tablespoon',
'name' => $this
->randomMachineName(16),
'note' => '',
];
$test_ingredients[] = [
'quantity' => '2 2/3',
'unit_key' => 'tablespoon',
'name' => $this
->randomMachineName(16),
'note' => '',
];
foreach ($test_ingredients as $ingredient) {
$title = $this
->randomMachineName(16);
$edit = [
'title[0][value]' => $title,
'field_ingredient[0][quantity]' => $ingredient['quantity'],
'field_ingredient[0][unit_key]' => $ingredient['unit_key'],
'field_ingredient[0][target_id]' => $ingredient['name'],
'field_ingredient[0][note]' => $ingredient['note'],
];
$this
->drupalPostForm('node/add/test_bundle', $edit, 'Save');
$this
->assertSession()
->pageTextContains($title);
if ($ingredient['quantity'] === 0) {
$this
->assertSession()
->responseContains(new FormattableMarkup('<span class="quantity-unit"> <abbr title="Tablespoon">T</abbr>', []));
}
elseif ($ingredient['unit_key'] == 'unit') {
$this
->assertSession()
->responseContains(new FormattableMarkup('<span class="quantity-unit">@quantity</span>', [
'@quantity' => $ingredient['quantity'],
]));
}
else {
$this
->assertSession()
->pageTextContains(new FormattableMarkup('@quantity T', [
'@quantity' => $ingredient['quantity'],
]));
}
if ($ingredient['note'] === '') {
$this
->assertSession()
->pageTextContains(new FormattableMarkup('@name', [
'@name' => $ingredient['name'],
]));
$this
->assertSession()
->pageTextNotContains(new FormattableMarkup('@name (@note)', [
'@name' => $ingredient['name'],
'@note' => $ingredient['note'],
]));
}
else {
$this
->assertSession()
->pageTextContains(new FormattableMarkup('@name (@note)', [
'@name' => $ingredient['name'],
'@note' => $ingredient['note'],
]));
}
}
}
public function testIngredientFieldSettings() {
$field_settings = [
'unit_sets' => [
'us',
'si',
'common',
],
'default_unit' => '',
];
$this
->createIngredientField([], $field_settings);
$this
->drupalGet('admin/structure/types/manage/test_bundle/fields/node.test_bundle.field_ingredient');
$this
->assertSession()
->checkboxChecked('edit-settings-unit-sets-us');
$this
->assertSession()
->checkboxChecked('edit-settings-unit-sets-si');
$this
->assertSession()
->checkboxChecked('edit-settings-unit-sets-common');
$option_field = $this
->assertSession()
->optionExists('edit-settings-default-unit', '');
$this
->assertTrue($option_field
->hasAttribute('selected'), 'The blank default unit was selected.');
$this
->drupalGet('node/add/test_bundle');
$this
->assertEquals($this
->xpath("//option[@value='cup']")[0]
->getText(), 'cup (c)', 'Found an option from the U.S. customary unit set.');
$this
->assertEquals($this
->xpath("//option[@value='milliliter']")[0]
->getText(), 'milliliter (ml)', 'Found an option from the SI/Metric unit set.');
$this
->assertEquals($this
->xpath("//option[@value='tablespoon']")[0]
->getText(), 'tablespoon (T)', 'Found an option from the Common unit set.');
$option_field = $this
->assertSession()
->optionExists('edit-field-ingredient-0-unit-key', '');
$this
->assertTrue($option_field
->hasAttribute('selected'), 'The empty unit option was selected.');
$field_settings = [
'unit_sets' => [
'us',
'common',
],
'default_unit' => 'cup',
];
$this
->updateIngredientField($field_settings);
$this
->drupalGet('node/add/test_bundle');
$this
->assertEquals($this
->xpath("//option[@value='cup']")[0]
->getText(), 'cup (c)', 'Found an option from the U.S. customary unit set.');
$this
->assertEmpty($this
->xpath("//option[@value='milliliter']"), 'Did not find an option from the SI/Metric unit set.');
$this
->assertEquals($this
->xpath("//option[@value='tablespoon']")[0]
->getText(), 'tablespoon (T)', 'Found an option from the Common unit set.');
$option_field = $this
->assertSession()
->optionExists('edit-field-ingredient-0-unit-key', 'cup');
$this
->assertTrue($option_field
->hasAttribute('selected'), 'The default unit was selected.');
$field_settings = [
'unit_sets' => [],
'default_unit' => '',
];
$this
->updateIngredientField($field_settings);
$this
->drupalGet('node/add/test_bundle');
$this
->assertEquals($this
->xpath("//option[@value='cup']")[0]
->getText(), 'cup (c)', 'Found an option from the U.S. customary unit set.');
$this
->assertEquals($this
->xpath("//option[@value='milliliter']")[0]
->getText(), 'milliliter (ml)', 'Found an option from the SI/Metric unit set.');
$this
->assertEquals($this
->xpath("//option[@value='tablespoon']")[0]
->getText(), 'tablespoon (T)', 'Found an option from the Common unit set.');
}
public function testIngredientFormatterSettings() {
$this
->createIngredientField();
$this
->drupalGet('admin/structure/types/manage/test_bundle/display');
$this
->assertSession()
->pageTextContains('Link to ingredient: No');
$edit = [
'title[0][value]' => $this
->randomMachineName(16),
'field_ingredient[0][quantity]' => 4,
'field_ingredient[0][unit_key]' => 'tablespoon',
'field_ingredient[0][target_id]' => 'test ingredient',
'field_ingredient[0][note]' => '',
];
$this
->drupalGet('node/add/test_bundle');
$this
->drupalPostForm(NULL, $edit, 'Save');
$this
->assertSession()
->pageTextContains('4 T');
$this
->assertSession()
->pageTextContains('test ingredient');
$this
->assertSession()
->linkNotExists('test ingredient', 'Ingredient entity link is not displayed.');
$this
->updateIngredientField([], [], [
'link' => TRUE,
'unit_display' => 1,
]);
$this
->drupalGet('admin/structure/types/manage/test_bundle/display');
$this
->assertSession()
->pageTextContains('Link to ingredient: Yes');
$this
->drupalGet('node/1');
$this
->assertSession()
->pageTextContains('4 tablespoons');
$this
->assertSession()
->pageTextContains('test ingredient');
$this
->assertSession()
->linkExists('test ingredient', 0);
}
}