Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EWPP-258: Create Behat test for testing "API steps" #315

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
6 changes: 6 additions & 0 deletions behat.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ default:
- Drupal\Tests\oe_media\Behat\FeatureContext
- Drupal\Tests\oe_content\Behat\Content\CorporateContentContext
- Drupal\Tests\oe_content\Behat\Content\Node\EventContentContext
- Drupal\Tests\oe_content\Behat\Content\Venue\DefaultVenueContext
- Drupal\Tests\oe_content\Behat\Content\Organisation\StakeholderOrganisationContext
- Drupal\Tests\oe_content\Behat\Content\Contact\GeneralContactContext
- Drupal\Tests\oe_content\Behat\Content\Contact\PressContactContext
- Drupal\Tests\oe_content\Behat\Content\Node\ProjectContentContext
- Drupal\Tests\oe_content\Behat\Content\Node\CallForTendersContentContext
- Drupal\Tests\oe_content\Behat\Content\Node\OrganisationContentContext
- OpenEuropa\Behat\TransformationContext:
elements:
Internal organiser field: "#edit-oe-event-organiser-internal-0-target-id"
Expand Down
69 changes: 65 additions & 4 deletions tests/Behat/DateFieldContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,74 @@ public function fillDateRangeSelectListField(string $field_item, string $field_g
}

/**
* Set the date and time value of a date list widget.
* Check values for list date range fields.
*
* When I set "Field" to the date "22-02-2019"
* When I set "Field" to the date "22-02-2019 14:30" using format "d-m-Y H:i"
* Then datetime "15 1 2020 12 30" is selected for "Start date" of "Datetime"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep the previous format. Also avoid passive forms as much as possible:

Suggested change
* Then datetime "15 1 2020 12 30" is selected for "Start date" of "Datetime"
* When I set "22-02-2019 14:30" as "start date" for "Field name"

*
* @param string $value
* The value of the field.
* @param string $field_item
* The date field item inside the field component.
* @param string $field_group
* The field component's label.
*
* @Then datetime :value is selected for :field_item of :field_group
* @Then date :value is selected for :field_item of :field_group
*/
public function isSelectedForOf($value, $field_item, $field_group): void {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type hinting are missing. Also in the other methods.

$page = $this->getSession()->getPage();
$values = explode(' ', $value);
$group = $page->find('named', ['fieldset', $field_group]);
$elements = $group->findAll('css', '.container-inline');

if (empty($elements)) {
throw new \Exception('Datetime fields not found.');
}

$fields = $elements[0];

if ($field_item === 'End date') {
$fields = $elements[1];
}

$date_components = [
'Day',
'Month',
'Year',
];

if (count($values) > 3) {
$date_components += [
'Hour',
'Minute',
];
}

foreach ($date_components as $index => $date_component) {
$field = $fields->findField($date_component);
$optionField = $field->find('named', [
'option',
$values[$index],
]);

if ($optionField === NULL) {
throw new \Exception(sprintf('%s option not found.', $date_component));
}

if (!$optionField->isSelected()) {
throw new \Exception(sprintf('%s not selected for %s option.', $values[$index], $date_component));
}
}
}

/**
* Finds a datetime field.
*
* @param string $field
* The label of the field.
* The field name.
* When I set "Field" to the date "22-02-2019".
* When I set "Field" to the date "22-02-2019 14:30" using
* format "d-m-Y H:i".
* @param string $value
* The value of the field.
* @param string $format
Expand Down
77 changes: 77 additions & 0 deletions tests/Behat/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,83 @@ protected function getLangcodeByName(string $language_name): string {
throw new \Exception("Language name '$language_name' is not valid.");
}

/**
* Visit a edit page by node title.
*
* @When I visit node :arg1 edit page
*/
public function iVisitNodeEditPage($arg1) {
$node = $this->getNodeByTitle($arg1);
$this->visitPath("/node/{$node->id()}/edit");
}

/**
* Checks, that form field has specified value.
*
* @Then the :field field contains :value
*/
public function theFieldContains($field, $value) {
$node = $this->getSession()->getPage()->findField($field);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to first assert that the field exists, before trying to access its value. You can use assert session for this.

$actual = $node->getValue();

if (strpos($actual, $value) === FALSE) {
throw new \Exception(sprintf('Field %s does not contain %s.', $field, $value));
}
}

/**
* Attempts to find a button in a table row containing giving text.
*
* @When I press :button in the :rowText row
*/
public function iPressInTheRow($button, $rowText) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing type hints and code sniffing shouldn't have passed here, as camel case is not allowed for method parameters. Check if that actually runs on these classes too.

$page = $this->getSession()->getPage();
$rows = $page->findAll('css', 'tr');

if (empty($rows)) {
throw new \Exception(sprintf('No rows found on the page %s', $this->getSession()->getCurrentUrl()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use assert sessions to check presence of elements.

}

/** @var \Behat\Mink\Element\NodeElement $row */
foreach ($rows as $row) {
if (strpos($row->getText(), $rowText) !== FALSE) {
break;
}
}

if ($row === NULL) {
throw new \Exception(sprintf('Row "%s" not found', $rowText));
}

$button_element = $row->findButton($button);

if ($button_element === NULL) {
throw new \Exception(sprintf('Button "%s" not found in row %s', $button, $rowText));
}

$button_element->press();
}

/**
* Assert option is selected.
*
* @When :option should be selected for :field select
*/
public function shouldBeSelectedForSelect($option, $field) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IF we wont use this let's remove it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use this method in the refactored Behat statements

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we have this already from the Drupal extension?

if (!$element = $this->getSession()->getPage()->findField($field)) {
throw new \Exception(sprintf('Field %s not found.', $field));
}

$selected_option = $element->find('css', "option[selected='selected']");

if ($selected_option === NULL) {
print_r($element->getHtml());
throw new \Exception(sprintf('Option "%s" not selected for %s select', $option, $field));
}

Assert::assertTrue($selected_option->getValue() === $option);
}

/**
* Step to fill in multi value fields with columns.
*
Expand Down
Loading