From 385150770d96f820c2e2cd29852646dec64229fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20L=C3=B3pez?= Date: Fri, 27 Sep 2024 14:48:43 +0200 Subject: [PATCH] Updated response code behat tests --- aljibe-kickstart/tests/behat/local/behat.yml | 2 +- .../tests/behat/local/features/200.feature | 24 ++- .../tests/behat/local/features/403.feature | 25 ++- .../tests/behat/local/features/404.feature | 17 +- aljibe-kickstart/tests/behat/pro/behat.yml | 122 +++++++++++++++ .../behat/pro/bootstrap/FeatureContext.php | 147 ++++++++++++++++++ .../tests/behat/pro/features/200.feature | 12 ++ .../tests/behat/pro/features/403.feature | 13 ++ .../tests/behat/pro/features/404.feature | 11 ++ .../tests/behat/pro/features/behat.feature | 14 ++ 10 files changed, 349 insertions(+), 38 deletions(-) create mode 100644 aljibe-kickstart/tests/behat/pro/behat.yml create mode 100644 aljibe-kickstart/tests/behat/pro/bootstrap/FeatureContext.php create mode 100644 aljibe-kickstart/tests/behat/pro/features/200.feature create mode 100644 aljibe-kickstart/tests/behat/pro/features/403.feature create mode 100644 aljibe-kickstart/tests/behat/pro/features/404.feature create mode 100644 aljibe-kickstart/tests/behat/pro/features/behat.feature diff --git a/aljibe-kickstart/tests/behat/local/behat.yml b/aljibe-kickstart/tests/behat/local/behat.yml index df5931b..679dcd8 100644 --- a/aljibe-kickstart/tests/behat/local/behat.yml +++ b/aljibe-kickstart/tests/behat/local/behat.yml @@ -62,7 +62,7 @@ default: ajax_timeout: 5 browserkit_http: ~ browser_name: "chrome" - base_url: "http://web" + base_url: "https://web" show_cmd: lynx %s files_path: "%paths.base%/tests/functional/behat/files" javascript_session: selenium2 diff --git a/aljibe-kickstart/tests/behat/local/features/200.feature b/aljibe-kickstart/tests/behat/local/features/200.feature index 8647707..25089da 100644 --- a/aljibe-kickstart/tests/behat/local/features/200.feature +++ b/aljibe-kickstart/tests/behat/local/features/200.feature @@ -1,18 +1,12 @@ -Feature: - As a Maintainer - I want to check that main pages are accesible +Feature: As a user + I want to visit the following pages + So that I can browse the site! - @behat - Scenario: Homepage is working - Given I go to "/" + Scenario Outline: 200 response + When I go to "" Then the response status code should be 200 - @behat - Scenario: Robots.txt is working - Given I go to "/robots.txt" - Then the response status code should be 200 - -# @behat -# Scenario: Sitemap is working -# Given I go to "/sitemap.xml" -# Then the response status code should be 200 \ No newline at end of file + Examples: + | url | + | / | + | /robots.txt | diff --git a/aljibe-kickstart/tests/behat/local/features/403.feature b/aljibe-kickstart/tests/behat/local/features/403.feature index 59bd23a..d8ab392 100644 --- a/aljibe-kickstart/tests/behat/local/features/403.feature +++ b/aljibe-kickstart/tests/behat/local/features/403.feature @@ -1,18 +1,13 @@ -Feature: - As a Maintainer - I want to check that blocked pages are not accesible +Feature: As a system + I want to block the following pages + So user cannot access where they should not! - @behat - Scenario: Admin area is not accesible - Given I go to "/admin" + Scenario Outline: 403 response + When I go to "" Then the response status code should be 403 - @behat - Scenario: User root profile page is not acccesible - Given I go to "/user/1" - Then the response status code should be 403 - - @behat - Scenario: User register page is not acccesible - Given I go to "/user/register" - Then the response status code should be 403 + Examples: + | url | + | /admin | + | /user/1 | + | /user/register | diff --git a/aljibe-kickstart/tests/behat/local/features/404.feature b/aljibe-kickstart/tests/behat/local/features/404.feature index 5e529a7..b69f90a 100644 --- a/aljibe-kickstart/tests/behat/local/features/404.feature +++ b/aljibe-kickstart/tests/behat/local/features/404.feature @@ -1,8 +1,11 @@ -Feature: - As a Maintainer - I want to check that some urls return not found code +Feature: As a system + I want to make not-found the following pages + So user cannot access where they should not! - @behat - Scenario: Homepage works - Given I go to "/url-not-working" - Then the response status code should be 404 \ No newline at end of file + Scenario Outline: 404 response + When I go to "" + Then the response status code should be 404 + + Examples: + | url | + | /url-not-found | diff --git a/aljibe-kickstart/tests/behat/pro/behat.yml b/aljibe-kickstart/tests/behat/pro/behat.yml new file mode 100644 index 0000000..39bc946 --- /dev/null +++ b/aljibe-kickstart/tests/behat/pro/behat.yml @@ -0,0 +1,122 @@ +default: + autoload: + "": "%paths.base%/bootstrap/" + formatters: + pretty: + verbose: false + paths: false + snippets: true + junit: + output_path: "%paths.base%/reports/behat/junit/" + suites: + default: + paths: + - "%paths.base%/features/" + filters: + tags: "~@exclude" + contexts: + - FeatureContext + - NuvoleWeb\Drupal\DrupalExtension\Context\CKEditorContext + - NuvoleWeb\Drupal\DrupalExtension\Context\PositionContext + - NuvoleWeb\Drupal\DrupalExtension\Context\ResponsiveContext: + devices: + laptop: 1200x800 + desktop: 2560x1440 + mobile_portrait: 370x650 + mobile_landscape: 650x370 + tablet_portrait: 800x1024 + tablet_landscape: 1024x768 + - Drupal\DrupalExtension\Context\MinkContext + - Drupal\DrupalExtension\Context\MessageContext + - Drupal\DrupalExtension\Context\MarkupContext + - Drupal\DrupalExtension\Context\DrushContext + - Metadrop\Behat\Context\CacheContext + - Metadrop\Behat\Context\ContentAuthoredContext + - Metadrop\Behat\Context\CronContext + - Metadrop\Behat\Context\DebugContext: + parameters: + "report_on_error": true + "error_reporting_path": "/var/www/html/web/sites/default/files/behat/errors" + "error_reporting_url": "http://example.ddev.site/sites/default/files/behat/errors" + "screenshots_url": "http://example.ddev.site/sites/default/files/behat/screenshots" + "screenshots_path": "/var/www/html/web/sites/default/files/behat/screenshots" + "page_contents_path": "/var/www/html/web/sites/default/files/behat/pages" + - Metadrop\Behat\Context\FileContext + - Metadrop\Behat\Context\FormContext + - Metadrop\Behat\Context\EntityContext + - Metadrop\Behat\Context\LogsContext: + parameters: + write_report: true + - Metadrop\Behat\Context\NodeAccessContext + - Metadrop\Behat\Context\ParagraphsContext + - Metadrop\Behat\Context\UIContext: + parameters: + scroll_offset: -100 + - Metadrop\Behat\Context\UrlContext + - Metadrop\Behat\Context\UsersContext + - Metadrop\Behat\Context\WaitingContext + - Metadrop\Behat\Context\MediaContext + + extensions: + Drupal\MinkExtension: + ajax_timeout: 5 + browserkit_http: ~ + browser_name: "chrome" + base_url: "https://example.com" + show_cmd: lynx %s + files_path: "%paths.base%/tests/functional/behat/files" + javascript_session: selenium2 + selenium2: + # wd_host: http://127.0.0.1:4444/wd/hub + wd_host: http://hub:4444/wd/hub + capabilities: + browser: "chrome" + extra_capabilities: + chromeOptions: + w3c: false + args: + - '--no-sandbox' + - '--start-maximized' + - '--disable-gpu' + - '--disable-web-security' + - '--DNS-prefetch-disable' + - '--disable-translate' + - '--ignore-certificate-errors' + - '--test-type' + - '--disable-extensions' + - '--incognito' + - '--disable-infobars' + NuvoleWeb\Drupal\DrupalExtension: + services: "/var/www/html/vendor/metadrop/behat-contexts/services.yml" + blackbox: ~ + api_driver: "drupal" + drupal: + # Change this to the absolute path to Drupal install. + drupal_root: "/var/www/html/web" + region_map: + content: ".region-content" + content bottom: ".region-content-bottom" + footer: "footer" + header: ".region-header" + header extra: ".region-header-extra" + highlighted: ".region-highlighted" + sidebar responsive: ".region-sidebar-responsive" + sidebar: ".region-sidebar-left" + fixed: ".region-fixed" + content top: ".region-content-top" + table revision: ".node-revision-table" + breadcrumb: ".breadcrumb" + selectors: + message_selector: ".messages" + success_message_selector: ".messages--success" + error_message_selector: ".messages--error" + warning_message_selector: ".messages--warning" + text: + password_field: "edit-pass" + username_field: "edit-name" + log_in: "Log in" + log_out: "Log out" + node_submit_label: "Save and publish" + Bex\Behat\StepTimeLoggerExtension: + enabled_always: true + output: [console, csv] diff --git a/aljibe-kickstart/tests/behat/pro/bootstrap/FeatureContext.php b/aljibe-kickstart/tests/behat/pro/bootstrap/FeatureContext.php new file mode 100644 index 0000000..850145d --- /dev/null +++ b/aljibe-kickstart/tests/behat/pro/bootstrap/FeatureContext.php @@ -0,0 +1,147 @@ + [ + * "edit any article content" => [RoleInterface::ANONYMOUS_ID], + * ], + * "revoke" => [ + * "access content" => [RoleInterface::ANONYMOUS_ID], + * ], + * ]; + */ + static protected $permissions = []; + + /** + * Sets User permissions. + * + * @BeforeSuite + */ + public static function beforeSuiteUserPermissionsSet() { + $roles = Role::loadMultiple(); + foreach (self::$permissions as $type => $value) { + foreach ($value as $permission => $rolesList) { + foreach ($rolesList as $role) { + $roles[$role]->{$type . 'Permission'}($permission); + $roles[$role]->trustData()->save(); + } + } + } + } + + /** + * Restores User permissions. + * + * @AfterSuite + */ + public static function afterUserPermissionsRestore() { + $roles = Role::loadMultiple(); + foreach (self::$permissions as $type => $value) { + foreach ($value as $permission => $rolesList) { + foreach ($rolesList as $role) { + $utype = $type == 'grant' ? 'revoke' : 'grant'; + $roles[$role]->{$utype . 'Permission'}($permission); + $roles[$role]->trustData()->save(); + } + } + } + } + + /** + * Retrieve all required sub-contexts. + * + * @BeforeScenario + */ + public function gatherContexts(BeforeScenarioScope $scope) { + $environment = $scope->getEnvironment(); + $this->minkContext = $environment->getContext('Drupal\DrupalExtension\Context\MinkContext'); + $this->responsiveContext = $environment->getContext('NuvoleWeb\Drupal\DrupalExtension\Context\ResponsiveContext'); + } + + /** + * Gets context parameters if they are defined. + * + * @param mixed $parameters + * Context parameter. + */ + public function __construct($parameters = NULL) { + $this->customParameters = !empty($parameters) ? $parameters : []; + } + + /** + * Sets EU Cookie Compliance: Accept. + * + * @BeforeScenario @cookies-accept + */ + public function beforeSetCookiesAccept() { + // The cookie is always sent for the current domain. Since you didn't visit + // a page yet, the domain has not been set yet. At the moment there's no way + // to choose a domain for a cookie with mink. + // You need to make a request before sending a cookie. + $this->visitPath('/'); + $this->getSession()->setCookie("cookie-agreed", "2"); + } + + /** + * Sets EU Cookie Compliance: Reject. + * + * @BeforeScenario @cookies-reject + */ + public function beforeSetCookiesReject() { + $this->visitPath('/'); + $this->getSession()->setCookie("cookie-agreed", "0"); + } + + /** + * Clear cache. + * + * @BeforeScenario @cache-clear + */ + public function cacheClearTag() { + $this->assertCacheClear(); + } + + /** + * Before scenario sets the viewport to desktop on javascript tests. + * + * @BeforeScenario @javascript + */ + public function beforeSetViewportEditor() { + $this->responsiveContext->assertDeviceScreenResize('desktop'); + } + +} diff --git a/aljibe-kickstart/tests/behat/pro/features/200.feature b/aljibe-kickstart/tests/behat/pro/features/200.feature new file mode 100644 index 0000000..25089da --- /dev/null +++ b/aljibe-kickstart/tests/behat/pro/features/200.feature @@ -0,0 +1,12 @@ +Feature: As a user + I want to visit the following pages + So that I can browse the site! + + Scenario Outline: 200 response + When I go to "" + Then the response status code should be 200 + + Examples: + | url | + | / | + | /robots.txt | diff --git a/aljibe-kickstart/tests/behat/pro/features/403.feature b/aljibe-kickstart/tests/behat/pro/features/403.feature new file mode 100644 index 0000000..d8ab392 --- /dev/null +++ b/aljibe-kickstart/tests/behat/pro/features/403.feature @@ -0,0 +1,13 @@ +Feature: As a system + I want to block the following pages + So user cannot access where they should not! + + Scenario Outline: 403 response + When I go to "" + Then the response status code should be 403 + + Examples: + | url | + | /admin | + | /user/1 | + | /user/register | diff --git a/aljibe-kickstart/tests/behat/pro/features/404.feature b/aljibe-kickstart/tests/behat/pro/features/404.feature new file mode 100644 index 0000000..b69f90a --- /dev/null +++ b/aljibe-kickstart/tests/behat/pro/features/404.feature @@ -0,0 +1,11 @@ +Feature: As a system + I want to make not-found the following pages + So user cannot access where they should not! + + Scenario Outline: 404 response + When I go to "" + Then the response status code should be 404 + + Examples: + | url | + | /url-not-found | diff --git a/aljibe-kickstart/tests/behat/pro/features/behat.feature b/aljibe-kickstart/tests/behat/pro/features/behat.feature new file mode 100644 index 0000000..765f063 --- /dev/null +++ b/aljibe-kickstart/tests/behat/pro/features/behat.feature @@ -0,0 +1,14 @@ +Feature: + As a Developer + I want to run Behat tests + So that I can enjoy BDD! + + @behat + Scenario: Goutte driver works + Given I go to "/" + Then the response status code should be 200 + + @behat @javascript + Scenario: Selenium driver works + Given I go to "/" + Then I should see "Log in"