From b8d7d54e399b2ea96f61f997393df7717f0462df Mon Sep 17 00:00:00 2001
From: James Hall <james@agile.coop>
Date: Fri, 12 Nov 2021 16:08:28 +0000
Subject: [PATCH 1/8] Require the DTT lib.

---
 composer.json | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/composer.json b/composer.json
index d7442211c..97f4efc82 100644
--- a/composer.json
+++ b/composer.json
@@ -57,7 +57,8 @@
         "drupal/core-dev": "~9.0",
         "drupal/devel": "^4.0.1",
         "drupal/drupal-extension": "^4.0",
-        "phpro/grumphp": "^0.18.0"
+        "phpro/grumphp": "^0.18.0",
+        "weitzman/drupal-test-traits": "^1.5"
     },
     "conflict": {
         "drupal/drupal": "*"
-- 
GitLab


From 32a35c8a14db753e73d64f2261603fcb5aacdecc Mon Sep 17 00:00:00 2001
From: James Hall <james@agile.coop>
Date: Fri, 12 Nov 2021 16:18:31 +0000
Subject: [PATCH 2/8] Add PHPUnit configuration and basic test.

---
 phpunit.xml                                   | 45 +++++++++++++++++++
 .../zephyr/tests/ExistingSite/ExampleTest.php | 33 ++++++++++++++
 2 files changed, 78 insertions(+)
 create mode 100644 phpunit.xml
 create mode 100644 web/profiles/zephyr/tests/ExistingSite/ExampleTest.php

diff --git a/phpunit.xml b/phpunit.xml
new file mode 100644
index 000000000..04d1fbd6c
--- /dev/null
+++ b/phpunit.xml
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!-- Copy the samples below into your own phpunit.xml file.-->
+
+<!-- Using this project's bootstrap file allows tests in `ExistingSite`,
+    `ExistingSiteSelenium2DriverTest`, and `ExistingSiteWebDriverTest`
+     to run alongside core's test types. -->
+
+<!-- If you use the default `bootstrap-fast.php` and get 'class not
+     found' errors while running tests, head over to
+     https://gitlab.com/weitzman/drupal-test-traits/-/blob/master/src/bootstrap-fast.php
+     for explanation on how to register those classes.
+-->
+<phpunit bootstrap="vendor/weitzman/drupal-test-traits/src/bootstrap-fast.php" colors="true"
+         beStrictAboutTestsThatDoNotTestAnything="true"
+         beStrictAboutOutputDuringTests="true"
+         beStrictAboutChangesToGlobalState="true"
+         printerClass="\Drupal\Tests\Listeners\HtmlOutputPrinter">
+  <php>
+    <!--    <env name="DTT_BASE_URL" value="http://bitk-drupal.lndo.site"/>-->
+    <!-- Example BROWSERTEST_OUTPUT_DIRECTORY value: /tmp
+         Specify a temporary directory for storing debug images and html documents.
+         These artifacts get copied to /sites/simpletest/browser_output by BrowserTestBase. -->
+    <!--    <env name="BROWSERTEST_OUTPUT_DIRECTORY" value="/app/web/sites/simpletest/browser_output"/>-->
+    <!-- To disable deprecation testing completely uncomment the next line. -->
+    <!--    <env name="SYMFONY_DEPRECATIONS_HELPER" value="disabled"/>-->
+    <!-- Specify the default directory screenshots should be placed. -->
+    <!--<env name="DTT_SCREENSHOT_REPORT_DIRECTORY" value=""/>-->
+    <!-- Specify the default directory page captures should be placed.
+        When using the \Drupal\Tests\Listeners\HtmlOutputPrinter printerClass this will default to
+        /sites/simpletest/browser_output. If using another printer such as teamcity this must be defined.
+        -->
+    <!--    <env name="DTT_HTML_OUTPUT_DIRECTORY" value="/app/web/sites/simpletest/browser_output"/>-->
+
+    <!--    <env name="BROWSERTEST_OUTPUT_BASE_URL" value="http://bitk-drupal.lndo.site/"/>-->
+  </php>
+
+  <testsuites>
+    <testsuite name="existing-site">
+      <!-- Assumes tests are namespaced as \Drupal\Tests\custom_foo\ExistingSite. -->
+      <directory>./web/profiles/zephyr/tests/src/ExistingSite</directory>
+      <!--<directory>./web/profiles/custom/*/tests/src/ExistingSite</directory>-->
+    </testsuite>
+  </testsuites>
+</phpunit>
diff --git a/web/profiles/zephyr/tests/ExistingSite/ExampleTest.php b/web/profiles/zephyr/tests/ExistingSite/ExampleTest.php
new file mode 100644
index 000000000..315ee6d49
--- /dev/null
+++ b/web/profiles/zephyr/tests/ExistingSite/ExampleTest.php
@@ -0,0 +1,33 @@
+<?php
+
+namespace Drupal\Tests\zephyr\ExistingSite;
+
+use weitzman\DrupalTestTraits\ExistingSiteBase;
+
+/**
+ * A model test case using traits from Drupal Test Traits.
+ */
+class ExampleTest extends ExistingSiteBase {
+
+  /**
+   * An example test method; note that Drupal API's and Mink are available.
+   *
+   * @throws \Drupal\Core\Entity\EntityStorageException
+   * @throws \Drupal\Core\Entity\EntityMalformedException
+   * @throws \Behat\Mink\Exception\ExpectationException
+   */
+  public function testBasicPage() {
+    $author = $this->createUser([], NULL, TRUE);
+    $node = $this->createNode([
+      'title' => 'A new page to test body content',
+      'type' => 'page',
+      'uid' => $author->id(),
+      'status' => 0,
+    ]);
+    $this->drupalLogin($author);
+    $this->drupalGet($node->toUrl());
+    $this->assertSession()->statusCodeEquals(200);
+    $this->assertSession()->responseContains('<p>A new page to test body content</p>');
+  }
+
+}
-- 
GitLab


From e28e23059ad82b973a7e6b4bb10279ebf3396e7e Mon Sep 17 00:00:00 2001
From: James Hall <james@agile.coop>
Date: Fri, 12 Nov 2021 16:21:22 +0000
Subject: [PATCH 3/8] Add boilerplate Gitlab CI.

---
 web/.gitlab-ci.yml.dist | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
 create mode 100644 web/.gitlab-ci.yml.dist

diff --git a/web/.gitlab-ci.yml.dist b/web/.gitlab-ci.yml.dist
new file mode 100644
index 000000000..2147589e6
--- /dev/null
+++ b/web/.gitlab-ci.yml.dist
@@ -0,0 +1,35 @@
+stages:
+  - test
+
+functional-test:
+  stage: test
+  image: registry.gitlab.com/a5997/bitk-drupal
+  services:
+    - mysql
+  variables:
+    MYSQL_USER: drupal
+    MYSQL_PASSWORD: drupal
+    MYSQL_DATABASE: drupal
+    MYSQL_ROOT_PASSWORD: root
+    DTT_BASE_URL: 'http://localhost/'
+    BROWSERTEST_OUTPUT_DIRECTORY: '/var/www/bitk/web/sites/simpletest/browser_output'
+    SYMFONY_DEPRECATIONS_HELPER: 'disabled'
+    DTT_HTML_OUTPUT_DIRECTORY: '/var/www/bitk/web/sites/simpletest/browser_output'
+    BROWSERTEST_OUTPUT_BASE_URL: 'http://localhost/'
+  # cache:
+  #   key: ${CI_COMMIT_REF_SLUG}
+  #   paths:
+  #     - .composer-cache-dir
+  script:
+    - ln -s $CI_PROJECT_DIR /var/www/bitk
+    - cd /var/www/bitk
+    - composer install
+    - mv assets/gitlab-ci/settings.local.php web/sites/default/settings.local.php
+    - bin/drush si bitk --existing-config
+    - mkdir -p web/sites/simpletest/browser_output && chmod -R 777 web/sites/simpletest
+    - chown -R docker:docker /var/www/bitk
+    - sed -i "s#www-data#docker#" /etc/apache2/envvars
+    - chown -R docker:docker web
+    - service apache2 restart
+    - chmod 777 ./assets/gitlab-ci/run-tests.sh
+    - bash ./assets/gitlab-ci/run-tests.sh
-- 
GitLab


From 239d0b556654915be7547da5a6984e603513d749 Mon Sep 17 00:00:00 2001
From: James Hall <james@agile.coop>
Date: Mon, 15 Nov 2021 11:36:24 +0000
Subject: [PATCH 4/8] Adds Docker assets

---
 assets/docker/Dockerfile                      | 66 +++++++++++++++++++
 assets/docker/docker.conf                     |  6 ++
 assets/docker/vhost.conf                      | 21 ++++++
 .../gitlab-ci/.gitlab-ci.yml                  | 21 +++---
 assets/gitlab-ci/run-tests.sh                 | 16 +++++
 assets/gitlab-ci/settings.local.php           |  0
 .../zephyr/tests/Functional/ZephyrTest.php    | 46 -------------
 7 files changed, 119 insertions(+), 57 deletions(-)
 create mode 100644 assets/docker/Dockerfile
 create mode 100644 assets/docker/docker.conf
 create mode 100644 assets/docker/vhost.conf
 rename web/.gitlab-ci.yml.dist => assets/gitlab-ci/.gitlab-ci.yml (65%)
 create mode 100644 assets/gitlab-ci/run-tests.sh
 create mode 100644 assets/gitlab-ci/settings.local.php
 delete mode 100644 web/profiles/zephyr/tests/Functional/ZephyrTest.php

diff --git a/assets/docker/Dockerfile b/assets/docker/Dockerfile
new file mode 100644
index 000000000..87ea0eeaa
--- /dev/null
+++ b/assets/docker/Dockerfile
@@ -0,0 +1,66 @@
+##
+# Zephyr Drupal web container
+
+FROM php:7.4-apache
+
+# Install PHP and related packages.
+RUN apt-get update && \
+    apt-get install -y \
+      curl \
+      git \
+      libcurl4-openssl-dev \
+      libfreetype6-dev \
+      libicu-dev \
+      libjpeg62-turbo-dev \
+      libonig-dev \
+      libpng-dev \
+      libxml2-dev \
+      libzip-dev \
+      mariadb-client \
+      patch \
+      zlib1g-dev && \
+    docker-php-ext-configure gd --with-freetype --with-jpeg  && \
+    docker-php-ext-install -j$(nproc) gd && \
+    docker-php-ext-install \
+      bcmath \
+      curl \
+      gettext \
+      intl \
+      mbstring \
+      mysqli \
+      pdo \
+      pdo_mysql \
+      zip  && \
+    apt-get clean && \
+    docker-php-source delete && \
+    rm -rf /tmp/* /var/cache/*
+
+# Install Composer.
+RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
+
+# Add a docker user.
+RUN useradd -m -s /bin/bash -G www-data -p docker docker
+
+# Configure Apache.
+ENV APACHE_RUN_USER docker
+ENV APACHE_RUN_GROUP docker
+ENV APACHE_LOG_DIR /var/log/apache2
+ENV APACHE_LOCK_DIR /var/lock/apache2
+ENV APACHE_RUN_DIR /var/run/apache2
+ENV APACHE_PID_FILE /var/run/apache2/apache2.pid
+COPY ./docker.conf /etc/apache2/conf-available/docker.conf
+COPY ./vhost.conf /etc/apache2/sites-available/vhost.conf
+RUN a2enmod \
+      expires \
+      headers \
+      rewrite && \
+    a2dissite 000-default.conf && \
+    a2ensite vhost.conf && \
+    ln -s /etc/apache2/conf-available/docker.conf /etc/apache2/conf-enabled/docker.conf
+
+# Configure PHP.
+#COPY assets/docker/php/docker.ini /usr/local/etc/php/conf.d/localgovdrupal.ini
+
+EXPOSE 80
+
+CMD ["/usr/sbin/apache2", "-D", "FOREGROUND"]
diff --git a/assets/docker/docker.conf b/assets/docker/docker.conf
new file mode 100644
index 000000000..0422454e9
--- /dev/null
+++ b/assets/docker/docker.conf
@@ -0,0 +1,6 @@
+##
+# Zephyr Drupal CI container
+#
+# Override default Apache config.
+
+ServerName drupal.localhost
diff --git a/assets/docker/vhost.conf b/assets/docker/vhost.conf
new file mode 100644
index 000000000..f9f789c33
--- /dev/null
+++ b/assets/docker/vhost.conf
@@ -0,0 +1,21 @@
+##
+# Zephyr Drupal CI container
+#
+# Default Apache virtual host.
+
+<VirtualHost *:80>
+
+    ServerAdmin docker
+    DocumentRoot "/var/www/bitk/web"
+
+    LogLevel warn
+    ErrorLog /var/log/apache2/drupal-error.log
+    CustomLog /var/log/apache2/drupal-access.log combined
+
+    <Directory /var/www/bitk/web>
+        Options Indexes FollowSymLinks MultiViews
+        AllowOverride All
+        Require all granted
+    </Directory>
+
+</VirtualHost>
diff --git a/web/.gitlab-ci.yml.dist b/assets/gitlab-ci/.gitlab-ci.yml
similarity index 65%
rename from web/.gitlab-ci.yml.dist
rename to assets/gitlab-ci/.gitlab-ci.yml
index 2147589e6..6e65e2c33 100644
--- a/web/.gitlab-ci.yml.dist
+++ b/assets/gitlab-ci/.gitlab-ci.yml
@@ -3,7 +3,9 @@ stages:
 
 functional-test:
   stage: test
-  image: registry.gitlab.com/a5997/bitk-drupal
+  image: gitlab.agile.coop:5050/agile-public/zephyr
+  only:
+    - merge_requests
   services:
     - mysql
   variables:
@@ -12,22 +14,19 @@ functional-test:
     MYSQL_DATABASE: drupal
     MYSQL_ROOT_PASSWORD: root
     DTT_BASE_URL: 'http://localhost/'
-    BROWSERTEST_OUTPUT_DIRECTORY: '/var/www/bitk/web/sites/simpletest/browser_output'
+    BROWSERTEST_OUTPUT_DIRECTORY: '/var/www/html/web/sites/simpletest/browser_output'
     SYMFONY_DEPRECATIONS_HELPER: 'disabled'
-    DTT_HTML_OUTPUT_DIRECTORY: '/var/www/bitk/web/sites/simpletest/browser_output'
+    DTT_HTML_OUTPUT_DIRECTORY: '/var/www/html/web/sites/simpletest/browser_output'
     BROWSERTEST_OUTPUT_BASE_URL: 'http://localhost/'
-  # cache:
-  #   key: ${CI_COMMIT_REF_SLUG}
-  #   paths:
-  #     - .composer-cache-dir
   script:
-    - ln -s $CI_PROJECT_DIR /var/www/bitk
-    - cd /var/www/bitk
+    - rm -rf /var/www/html
+    - ln -s $CI_PROJECT_DIR /var/www/html
+    - cd /var/www/html
     - composer install
     - mv assets/gitlab-ci/settings.local.php web/sites/default/settings.local.php
-    - bin/drush si bitk --existing-config
+    - bin/drush si zephyr --existing-config
     - mkdir -p web/sites/simpletest/browser_output && chmod -R 777 web/sites/simpletest
-    - chown -R docker:docker /var/www/bitk
+    - chown -R docker:docker /var/www/html
     - sed -i "s#www-data#docker#" /etc/apache2/envvars
     - chown -R docker:docker web
     - service apache2 restart
diff --git a/assets/gitlab-ci/run-tests.sh b/assets/gitlab-ci/run-tests.sh
new file mode 100644
index 000000000..59e1b3471
--- /dev/null
+++ b/assets/gitlab-ci/run-tests.sh
@@ -0,0 +1,16 @@
+#!/bin/bash
+
+##
+# Run all tests!
+RESULT=0
+
+# PHPUnit tests.
+echo "Running tests"
+su docker -c "./bin/phpunit"
+if [ $? -ne 0 ]; then
+  ((RESULT++))
+fi
+
+echo $RESULT
+# Set return code depending on number of tests that failed.
+exit $RESULT
diff --git a/assets/gitlab-ci/settings.local.php b/assets/gitlab-ci/settings.local.php
new file mode 100644
index 000000000..e69de29bb
diff --git a/web/profiles/zephyr/tests/Functional/ZephyrTest.php b/web/profiles/zephyr/tests/Functional/ZephyrTest.php
deleted file mode 100644
index 59c25e4a7..000000000
--- a/web/profiles/zephyr/tests/Functional/ZephyrTest.php
+++ /dev/null
@@ -1,46 +0,0 @@
-<?php
-
-namespace Drupal\Tests\standard\Functional;
-
-use Drupal\Component\Utility\Html;
-use Drupal\media\Entity\MediaType;
-use Drupal\media\Plugin\media\Source\Image;
-use Drupal\Tests\SchemaCheckTestTrait;
-use Drupal\contact\Entity\ContactForm;
-use Drupal\Core\Url;
-use Drupal\dynamic_page_cache\EventSubscriber\DynamicPageCacheSubscriber;
-use Drupal\filter\Entity\FilterFormat;
-use Drupal\Tests\BrowserTestBase;
-use Drupal\Tests\RequirementsPageTrait;
-use Drupal\user\Entity\Role;
-
-/**
- * Tests Standard installation profile expectations.
- *
- * @group standard
- */
-class ZephyrTest extends BrowserTestBase {
-
-  use SchemaCheckTestTrait;
-  use RequirementsPageTrait;
-
-  protected $profile = 'zephyr';
-
-  protected $defaultTheme = 'bartik';
-
-  /**
-   * The admin user.
-   *
-   * @var \Drupal\user\UserInterface
-   */
-  protected $adminUser;
-
-  /**
-   * Tests Zephyr installation profile.
-   */
-  public function testZephyr() {
-    $this->drupalGet('');
-    $this->assertResponse(200);
-  }
-
-}
-- 
GitLab


From 28f3128fe352fb28a5cf885088bfd33e7dc2e017 Mon Sep 17 00:00:00 2001
From: James <james@agile.coop>
Date: Mon, 15 Nov 2021 13:16:29 +0000
Subject: [PATCH 5/8] Update email_registration settings to be compatible with
 functional tests

Functional tests expect a username field when logging in so configuring email_registration to allow users to use either username or email to log in make it compatible.
---
 drupal-config/default/email_registration.settings.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drupal-config/default/email_registration.settings.yml b/drupal-config/default/email_registration.settings.yml
index bf7aae332..f41c9f444 100644
--- a/drupal-config/default/email_registration.settings.yml
+++ b/drupal-config/default/email_registration.settings.yml
@@ -1,3 +1,3 @@
-login_with_username: false
+login_with_username: true
 _core:
   default_config_hash: 6cFmNHjhs_glkuywBEhzcUQ8aiHXcFe6hZfW7PLN-qg
-- 
GitLab


From fecde98eb5ab8be9a159451b14b243246a6ac702 Mon Sep 17 00:00:00 2001
From: James Hall <james@agile.coop>
Date: Mon, 15 Nov 2021 13:22:59 +0000
Subject: [PATCH 6/8] Move the exsiting test into the proper namespaced
 directory

---
 .../zephyr/tests/{ => src}/ExistingSite/ExampleTest.php      | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
 rename web/profiles/zephyr/tests/{ => src}/ExistingSite/ExampleTest.php (85%)

diff --git a/web/profiles/zephyr/tests/ExistingSite/ExampleTest.php b/web/profiles/zephyr/tests/src/ExistingSite/ExampleTest.php
similarity index 85%
rename from web/profiles/zephyr/tests/ExistingSite/ExampleTest.php
rename to web/profiles/zephyr/tests/src/ExistingSite/ExampleTest.php
index 315ee6d49..4ed696578 100644
--- a/web/profiles/zephyr/tests/ExistingSite/ExampleTest.php
+++ b/web/profiles/zephyr/tests/src/ExistingSite/ExampleTest.php
@@ -19,15 +19,16 @@ class ExampleTest extends ExistingSiteBase {
   public function testBasicPage() {
     $author = $this->createUser([], NULL, TRUE);
     $node = $this->createNode([
-      'title' => 'A new page to test body content',
+      'title' => 'Test me!',
       'type' => 'page',
       'uid' => $author->id(),
       'status' => 0,
     ]);
+    $this->assertSession()->statusCodeEquals(403);
     $this->drupalLogin($author);
     $this->drupalGet($node->toUrl());
     $this->assertSession()->statusCodeEquals(200);
-    $this->assertSession()->responseContains('<p>A new page to test body content</p>');
+    $this->assertSession()->responseContains('Test me!');
   }
 
 }
-- 
GitLab


From 721de3b02688bd7982fb34ed676d65375d7e5612 Mon Sep 17 00:00:00 2001
From: James <james@agile.coop>
Date: Mon, 15 Nov 2021 13:46:08 +0000
Subject: [PATCH 7/8] Update basic page test

Previously, we were incorrectly testing the status code before we had navigated to the newly created node.
---
 web/profiles/zephyr/tests/src/ExistingSite/ExampleTest.php | 1 +
 1 file changed, 1 insertion(+)

diff --git a/web/profiles/zephyr/tests/src/ExistingSite/ExampleTest.php b/web/profiles/zephyr/tests/src/ExistingSite/ExampleTest.php
index 4ed696578..7d2b11550 100644
--- a/web/profiles/zephyr/tests/src/ExistingSite/ExampleTest.php
+++ b/web/profiles/zephyr/tests/src/ExistingSite/ExampleTest.php
@@ -24,6 +24,7 @@ class ExampleTest extends ExistingSiteBase {
       'uid' => $author->id(),
       'status' => 0,
     ]);
+    $this->drupalGet($node->toUrl());
     $this->assertSession()->statusCodeEquals(403);
     $this->drupalLogin($author);
     $this->drupalGet($node->toUrl());
-- 
GitLab


From e9571a8eacc41245ac454824cf7fce7968748d32 Mon Sep 17 00:00:00 2001
From: James Hall <james@agile.coop>
Date: Mon, 15 Nov 2021 15:45:02 +0000
Subject: [PATCH 8/8] Update example test to check for redirect to log in page

---
 web/profiles/zephyr/tests/src/ExistingSite/ExampleTest.php | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/web/profiles/zephyr/tests/src/ExistingSite/ExampleTest.php b/web/profiles/zephyr/tests/src/ExistingSite/ExampleTest.php
index 7d2b11550..734cb67ff 100644
--- a/web/profiles/zephyr/tests/src/ExistingSite/ExampleTest.php
+++ b/web/profiles/zephyr/tests/src/ExistingSite/ExampleTest.php
@@ -25,11 +25,16 @@ class ExampleTest extends ExistingSiteBase {
       'status' => 0,
     ]);
     $this->drupalGet($node->toUrl());
-    $this->assertSession()->statusCodeEquals(403);
+    $this->assertTrue(str_contains($this->getUrl(), 'user/login'), 'Redirect to login');
     $this->drupalLogin($author);
     $this->drupalGet($node->toUrl());
+    $this->assertTrue(
+      $node->toUrl('canonical', ['absolute' => TRUE])->toString() == $this->getUrl(),
+      'On node page URL'
+    );
     $this->assertSession()->statusCodeEquals(200);
     $this->assertSession()->responseContains('Test me!');
+    $this->assertSession()->responseContains('Test me!');
   }
 
 }
-- 
GitLab