View source
<?php
namespace Drupal\Tests\recipe\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Url;
class RecipeNodeTest extends RecipeTestBase {
public function testRecipeContent() {
$title = $this
->randomMachineName(16);
$description = $this
->randomMachineName(255);
$yield_amount = 5;
$yield_unit = $this
->randomMachineName(10);
$source = 'http://www.example.com';
$notes = $this
->randomMachineName(255);
$instructions = $this
->randomMachineName(255);
$preptime = 60;
$cooktime = 135;
$ing_0_quantity = 1;
$ing_0_unit = 'tablespoon';
$ing_0_name = $this
->randomMachineName(16);
$ing_0_note = $this
->randomMachineName(16);
$edit = [
'title[0][value]' => $title,
'recipe_description[0][value]' => $description,
'recipe_yield_amount[0][value]' => $yield_amount,
'recipe_yield_unit[0][value]' => $yield_unit,
'recipe_source[0][value]' => $source,
'recipe_notes[0][value]' => $notes,
'recipe_instructions[0][value]' => $instructions,
'recipe_prep_time[0][value]' => $preptime,
'recipe_cook_time[0][value]' => $cooktime,
'recipe_ingredient[0][quantity]' => $ing_0_quantity,
'recipe_ingredient[0][unit_key]' => $ing_0_unit,
'recipe_ingredient[0][target_id]' => $ing_0_name,
'recipe_ingredient[0][note]' => $ing_0_note,
];
$this
->drupalPostForm('node/add/recipe', $edit, 'Save');
$this
->assertSession()
->pageTextContains(new FormattableMarkup('Recipe @title has been created.', [
'@title' => $title,
]));
$this
->assertSession()
->responseContains($description);
$this
->assertSession()
->pageTextContains(new FormattableMarkup('@amount @unit', [
'@amount' => $yield_amount,
'@unit' => $yield_unit,
]));
$this
->assertSession()
->responseContains('<a href="http://www.example.com">http://www.example.com</a>');
$this
->assertSession()
->responseContains($notes);
$this
->assertSession()
->responseContains($instructions);
$this
->assertSession()
->pageTextContains('1 hour');
$this
->assertSession()
->pageTextContains('2 hours, 15 minutes');
$this
->assertSession()
->pageTextContains('3 hours, 15 minutes');
$this
->assertSession()
->pageTextContains('1 T');
$this
->assertSession()
->pageTextContains(new FormattableMarkup('@name (@note)', [
'@name' => $ing_0_name,
'@note' => $ing_0_note,
]));
$properties = [
'schema:Recipe',
'schema:name',
'schema:recipeInstructions',
'schema:recipeIngredient',
'schema:description',
'schema:prepTime',
'schema:cookTime',
'schema:totalTime',
'schema:recipeYield',
];
foreach ($properties as $property) {
$this
->assertSession()
->responseContains($property);
}
$durations = [
'prep_time' => 'PT1H',
'cook_time' => 'PT2H15M',
'total_time' => 'PT3H15M',
];
foreach ($durations as $duration) {
$this
->assertSession()
->responseContains($duration);
}
$expected_breadcrumb = [];
$expected_breadcrumb[] = Url::fromRoute('<front>')
->toString();
$expected_breadcrumb[] = Url::fromRoute('recipe.landing_page')
->toString();
$links = $this
->xpath('//nav[@aria-labelledby="system-breadcrumb"]/ol/li/a');
$got_breadcrumb = [];
foreach ($links as $link) {
$got_breadcrumb[] = (string) $link
->getAttribute('href');
}
$this
->assertSame($expected_breadcrumb, $got_breadcrumb, 'The breadcrumb is correctly displayed on the page.');
}
public function testPseudoFields() {
$edit = [
'title[0][value]' => $this
->randomMachineName(16),
'recipe_yield_amount[0][value]' => 1,
'recipe_yield_unit[0][value]' => $this
->randomMachineName(16),
'recipe_prep_time[0][value]' => 1,
'recipe_cook_time[0][value]' => 1,
];
$this
->drupalPostForm('node/add/recipe', $edit, 'Save');
$this
->assertSession()
->pageTextContains('Yield');
$this
->assertSession()
->pageTextContains('Total time');
$edit = [
'title[0][value]' => $this
->randomMachineName(16),
'recipe_yield_unit[0][value]' => $this
->randomMachineName(16),
'recipe_cook_time[0][value]' => 1,
];
$this
->drupalPostForm('node/add/recipe', $edit, 'Save');
$this
->assertSession()
->pageTextNotContains('Yield');
$this
->assertSession()
->pageTextNotContains('Total time');
$edit = [
'title[0][value]' => $this
->randomMachineName(16),
];
$this
->drupalPostForm('node/add/recipe', $edit, 'Save');
$this
->assertSession()
->pageTextNotContains('Yield');
$this
->assertSession()
->pageTextNotContains('Total time');
$repository = \Drupal::service('entity_display.repository');
$repository
->getViewDisplay('node', 'recipe', 'default')
->removeComponent('recipe_yield')
->removeComponent('recipe_total_time')
->save();
$this
->drupalGet('node/1');
$this
->assertSession()
->pageTextNotContains('Yield');
$this
->assertSession()
->pageTextNotContains('Total time');
}
}