This commit is contained in:
Sara Arjona 2024-07-10 15:20:59 +02:00
commit 82f5cc6829
No known key found for this signature in database
5 changed files with 71 additions and 9 deletions

View File

@ -22,6 +22,7 @@ use behat_base;
use Behat\Gherkin\Parser;
use Behat\Gherkin\Lexer;
use Behat\Gherkin\Keywords\ArrayKeywords;
use Behat\Gherkin\Node\OutlineNode;
use ReflectionClass;
use ReflectionMethod;
use stdClass;
@ -170,8 +171,7 @@ class runner {
$scenarios = $feature->getScenarios();
foreach ($scenarios as $scenario) {
if ($scenario->getNodeType() == 'Outline') {
$result->add_scenario($scenario->getNodeType(), $scenario->getTitle());
$result->add_error(get_string('testscenario_outline', 'tool_generator'));
$this->parse_scenario_outline($scenario, $result);
continue;
}
$result->add_scenario($scenario->getNodeType(), $scenario->getTitle());
@ -184,6 +184,23 @@ class runner {
return $result;
}
/**
* Parse a scenario outline.
* @param OutlineNode $scenario the scenario outline to parse.
* @param parsedfeature $result the parsed feature to add the scenario.
*/
private function parse_scenario_outline(OutlineNode $scenario, parsedfeature $result) {
$count = 1;
foreach ($scenario->getExamples() as $example) {
$result->add_scenario($example->getNodeType(), $example->getOutlineTitle() . " ($count)");
$steps = $example->getSteps();
foreach ($steps as $step) {
$result->add_step(new steprunner(null, $this->validsteps, $step));
}
$count++;
}
}
/**
* Get the parser.
* @return Parser

View File

@ -60,10 +60,17 @@ Feature: Create testing scenarios using generators
Then I should see "The file format is not valid or contains invalid steps"
@javascript
Scenario: Prevent creating a testing scenario from a scenario outline
Scenario: Create a testing scenario from a scenario outline
Given I log in as "admin"
And I navigate to "Development > Create testing scenarios" in site administration
When I upload "admin/tool/generator/tests/fixtures/testscenario/scenario_wrongoutline.feature" file to "Feature file" filemanager
When I upload "admin/tool/generator/tests/fixtures/testscenario/scenario_outline.feature" file to "Feature file" filemanager
And I press "Import"
Then I should see "Scenario outlines are not supported"
Then I should see "There are no steps to execute in the file"
And I should see "Example: creating test scenarios using an outline (1)"
And I should see "Example: creating test scenarios using an outline (2)"
And I should see "Example: creating test scenarios using an outline (3)"
Then I am on the "C1" "Course" page
And I should see "Course 1" in the "page-header" "region"
And I am on the "C2" "Course" page
And I should see "Course 2" in the "page-header" "region"
And I am on the "C3" "Course" page
And I should see "Course 3" in the "page-header" "region"

View File

@ -1,4 +1,4 @@
Feature: Prepare scenario for testing
Feature: Fixture to prepare scenario for testing
Scenario: Create course content
Given the following config values are set as admin:
| sendcoursewelcomemessage | 0 | enrol_manual |

View File

@ -1,6 +1,6 @@
Feature: Prepare scenario for testing
Feature: Fixture to prepare scenario for testing from an outline
Scenario Outline: test outline scenarios are not supported yet
Scenario Outline: creating test scenarios using an outline
Given the following "course" exists:
| fullname | <name> |
| shortname | <shortname> |

View File

@ -104,4 +104,42 @@ class runner_test extends \advanced_testcase {
$this->assertFalse($result);
$this->assertEquals(0, $DB->count_records('course', ['shortname' => 'C1']));
}
/**
* Test for parse_feature.
* @covers ::parse_feature
* @covers ::execute
*/
public function test_parse_and_execute_outline_feature(): void {
global $CFG, $DB;
$this->resetAfterTest();
$this->setAdminUser();
// Call the init method to include all behat libraries and attributes.
$runner = new runner();
$runner->init();
$featurefile = $CFG->dirroot . '/admin/tool/generator/tests/fixtures/testscenario/scenario_outline.feature';
$contents = file_get_contents($featurefile);
$feature = $runner->parse_feature($contents);
$this->assertEquals(3, count($feature->get_scenarios()));
$this->assertEquals(3, count($feature->get_all_steps()));
$this->assertTrue($feature->is_valid());
$result = $runner->execute($feature);
$this->assertTrue($result);
// Validate everything is created.
$course = $DB->get_record('course', ['shortname' => 'C1']);
$this->assertEquals('C1', $course->shortname);
$this->assertEquals('Course 1', $course->fullname);
$course = $DB->get_record('course', ['shortname' => 'C2']);
$this->assertEquals('C2', $course->shortname);
$this->assertEquals('Course 2', $course->fullname);
$course = $DB->get_record('course', ['shortname' => 'C3']);
$this->assertEquals('C3', $course->shortname);
$this->assertEquals('Course 3', $course->fullname);
}
}