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

[OPS-10828] Add tests #1

Merged
merged 4 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 19 additions & 27 deletions .github/workflows/test.yml → .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
name: Test
name: Run tests

on: [pull_request]

env:
MYSQL_USER: root
MYSQL_PASSWORD: root
Expand All @@ -19,15 +21,15 @@ jobs:
php-versions: ['8.3']
steps:
- name: Checkout Drupal core
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
repository: drupal/drupal
ref: ${{ matrix.drupal-core }}

- name: Checkout module
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
path: modules/custom/ocha_uimc
path: modules/contrib/ocha_uimc

- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2
Expand All @@ -37,36 +39,26 @@ jobs:

- name: Get composer cache directory
id: composercache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache composer dependencies
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: ${{ steps.composercache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install Drupal core dependencies
run: |
composer --no-interaction --no-progress --prefer-dist --optimize-autoloader install

- name: Install module dependencies
run: |
composer --no-interaction --no-progress require \
drupal/datetime_range_timezone drupal/date_recur

- name: Patch Drupal - https://www.drupal.org/project/drupal/issues/3004425
run: |
curl https://www.drupal.org/files/issues/2021-12-25/3004425-41.patch | patch -p1
- name: Remove platform PHP config
run: composer config --unset platform.php

- name: Install Coder module
- name: Install Drupal core and module dependencies
run: |
composer --dev --no-interaction --no-progress require \
drupal/coder:8.3.13 phpunit/phpunit
composer config repositories.module '{"type": "path", "url": "modules/contrib/ocha_uimc", "options": {"symlink": false}}'
composer require unocha/ocha_uimc:@dev --no-interaction --no-progress --prefer-dist --optimize-autoloader

- name: Define DRUPAL_ROOT env variable
run: |
echo "DRUPAL_ROOT=$HOME/drupal" >> $GITHUB_ENV
echo "DRUPAL_ROOT=$GITHUB_WORKSPACE" >> $GITHUB_ENV

- name: Install drush
run: composer require "drush/drush ^12.0"
Expand All @@ -79,15 +71,15 @@ jobs:
run: |
php -d sendmail_path=$(which true); vendor/bin/drush --yes -v \
site-install minimal --db-url="$SIMPLETEST_DB"
vendor/bin/drush en $DRUPAL_MODULE_NAME -y
vendor/bin/drush en ocha_uimc -y

- name: Run tests
- name: Run module tests
run: |
vendor/bin/phpunit --bootstrap core/tests/bootstrap.php \
XDEBUG_MODE=coverage php -d zend_extension=xdebug ./vendor/bin/phpunit --bootstrap core/tests/bootstrap.php \
--coverage-clover ./clover.xml \
-c modules/custom/ocha_uimc/phpunit.xml modules/custom/ocha_uimc
-c modules/contrib/ocha_uimc/phpunit.xml modules/contrib/ocha_uimc

- name: Monitor coverage for Drupal ${{ matrix.drupal-core }} - PHP ${{ matrix.php-versions }}
- name: Monitor coverage
id: coveralls
uses: slavcodev/coverage-monitor-action@v1
with:
Expand Down
4 changes: 4 additions & 0 deletions config/install/ocha_uimc.settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ username: ''
password: ''
consumer_key: ''
consumer_secret: ''
send_email: true
verify_ssl: true
request_timeout: 10
registration_explanation: |
<p>Create an account to easily access our services.<br>If you already possess a UN email address, please <a href="/user/login">log in here</a> directly.</p>
11 changes: 10 additions & 1 deletion config/schema/ocha_uimc.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ ocha_uimc.settings:
consumer_secret:
type: string
label: 'The API consumer secret.'
send_email:
type: boolean
label: 'Whether or not have an email sent after registration.'
verify_ssl:
type: bool
type: boolean
label: 'Whether to verify SSL or not when querying the API. Useful for local/dev tests.'
request_timeout:
type: integer
label: 'Timeout for requests to the UIMC API.'
registration_explanation:
type: string
label: 'Explanation about the registration. Simple HTML is accepted.'
4 changes: 2 additions & 2 deletions ocha_uimc.services.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
services:
ocha_uimc.api.client:
class: Drupal\ocha_uimc\Services\OchaUimcApiClient
class: Drupal\ocha_uimc\Service\OchaUimcApiClient
arguments:
- '@http_client'
- '@config.factory'
- '@key.repository'
- '@logger_channel.factory'
- '@logger.factory'
- '@datetime.time'
14 changes: 14 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="drupal">
<description>PHP CodeSniffer configuration for Drupal coding standards.</description>
<file>.</file>
<arg name="extensions" value="php,module,inc,install,test,profile,theme,css,info,txt,md,yml" />
<config name="drupal_core_version" value="10" />
<exclude-pattern>./vendor/*</exclude-pattern>
<exclude-pattern>./.composer/*</exclude-pattern>
<exclude-pattern>*.md</exclude-pattern>
<exclude-pattern>*.css</exclude-pattern>
<exclude-pattern>*.js</exclude-pattern>
<rule ref="Drupal" />
<rule ref="DrupalPractice" />
</ruleset>
39 changes: 39 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit colors="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutChangesToGlobalState="true"
failOnWarning="true"
cacheResult="false">
<php>
<ini name="error_reporting" value="32767"/>
<ini name="memory_limit" value="-1"/>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak"/>
<env name="BROWSERTEST_OUTPUT_BASE_URL" value=""/>
</php>
<coverage includeUncoveredFiles="false"
processUncoveredFiles="false"
pathCoverage="false"
ignoreDeprecatedCodeUnits="true"
disableCodeCoverageIgnore="false">
<include>
<!-- Relative to this file! -->
<directory suffix=".php">src</directory>
<directory suffix=".module">.</directory>
</include>
<exclude>
<!-- Relative to this file! -->
<directory suffix="Interface.php">src</directory>
<directory>tests</directory>
</exclude>
<report>
<clover outputFile="clover.xml"/>
</report>
</coverage>
<testsuites>
<testsuite name="unit">
<directory>tests/src/Unit</directory>
</testsuite>
</testsuites>
</phpunit>
18 changes: 16 additions & 2 deletions src/Form/OchaUimcRegistrationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;
use Drupal\honeypot\HoneypotService;
use Drupal\ocha_uimc\Service\OchaUimcApiClientInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
Expand Down Expand Up @@ -42,12 +43,24 @@ public static function create(ContainerInterface $container) {
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
// Add the registration explanation message.
$explanation = $this->config('ocha_uimc.settings')->get('registration_explanation');
if (!empty($explanation)) {
$form['registration_explanation'] = [
'#type' => 'markup',
'#markup' => Markup::create($explanation),
'#prefix' => '<div class="ocha-uimc-registration-explanation">',
'#suffix' => '</div>',
];
}

$form['first_name'] = [
'#type' => 'textfield',
'#title' => $this->t('First Name'),
'#required' => TRUE,
'#maxlength' => 30,
'#placeholder' => $this->t('Enter your first name'),
'#description' => $this->t('Enter your first name using only letters, spaces, hyphens, or apostrophes. Maximum 30 characters.'),
];

$form['last_name'] = [
Expand All @@ -56,6 +69,7 @@ public function buildForm(array $form, FormStateInterface $form_state): array {
'#required' => TRUE,
'#maxlength' => 30,
'#placeholder' => $this->t('Enter your last name'),
'#description' => $this->t('Enter your last name using only letters, spaces, hyphens, or apostrophes. Maximum 30 characters.'),
];

$form['email'] = [
Expand All @@ -64,6 +78,7 @@ public function buildForm(array $form, FormStateInterface $form_state): array {
'#required' => TRUE,
'#maxlength' => 100,
'#placeholder' => $this->t('Enter your email address'),
'#description' => $this->t('Enter a valid email address. Only letters, numbers, hyphens, and periods are allowed. Maximum 100 characters.'),
];

$form['actions']['#type'] = 'actions';
Expand Down Expand Up @@ -103,9 +118,8 @@ public function validateForm(array &$form, FormStateInterface $form_state): void
if (strlen($email) > 100 || preg_match('/^[a-zA-Z0-9.-]{1,64}@[a-zA-Z0-9.-]{1,255}$/', $email) !== 1) {
$form_state->setErrorByName('email', $this->t('Email must contain only letters, numbers, hyphens, or periods and be no longer than 100 characters.'));
}

// Additional email validation.
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
elseif (!filter_var($email, \FILTER_VALIDATE_EMAIL)) {
$form_state->setErrorByName('email', $this->t('Please enter a valid email address.'));
}
}
Expand Down
Loading
Loading