Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
trasher committed Jan 28, 2024
1 parent 319f58b commit 654a0b5
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 3 deletions.
99 changes: 98 additions & 1 deletion .github/workflows/ci-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:
pull_request:

jobs:
ubuntu-latest:
lint:
runs-on: ubuntu-latest

strategy:
Expand Down Expand Up @@ -84,3 +84,100 @@ jobs:
run: |
cd galette-core/galette/plugins/plugin-oauth2
../../vendor/bin/docheader check lib ./*.php
unit-tests:
runs-on: ubuntu-latest

strategy:
matrix:
include:
#lower php version
- { php-version: "8.1", db-image: "postgres:16", coverage: none, always: false }
#higher stable php version
- { php-version: "8.3", db-image: "postgres:16", coverage: none, always: true }
fail-fast: false

env:
DB: ${{ matrix.db-image }}

services:
# Label used to access the service container
db:
# Docker Hub image
image: ${{ matrix.db-image }}
# Provide env variables for both mysql and pgsql
env:
POSTGRES_USER: galette_tests
POSTGRES_PASSWORD: g@l3tte
POSTGRES_DB: galette_tests
# Open network ports for pgsql
ports:
- 5432:5432
# Set health checks to wait until postgres has started
options: >-
--health-cmd="bash -c 'pg_isready'"
--health-interval=10s
--health-timeout=5s
--health-retries=10
name: PHP ${{ matrix.php-version }} ${{ matrix.db-image }}

steps:
- name: PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
tools: composer, pecl
extensions: apcu
ini-values: apc.enable_cli=1

- name: "Show versions"
if: env.skip != 'true'
run: |
php --version
composer --version
echo "node $(node --version)"
echo "npm $(npm --version)"
docker exec ${{ job.services.db.id }} bash -c "psql --version"
- name: Checkout Galette core
uses: actions/checkout@v4
with:
repository: galette/galette
path: galette-core
fetch-depth: 1
ref: develop

- name: Checkout plugin
uses: actions/checkout@v4
with:
path: galette-core/galette/plugins/plugin-oauth2

- name: "Restore dependencies cache"
uses: actions/cache@v3
with:
path: |
~/.composer/cache/
~/.npm/_cacache/
key: "${{ runner.os }}-galette-${{ matrix.php-version }}-${{ hashFiles('galette/composer.lock', 'package-lock.json') }}"
restore-keys: |
${{ runner.os }}-galette-${{ matrix.php-version }}-
- name: Install dependencies
run: |
cd galette-core
bin/install_deps
cd galette/plugins/plugin-oauth2
composer install --ignore-platform-reqs
- name: Init for PostgreSQL
env:
POSTGRES_HOST: localhost
POSTGRES_PORT: 5432
run: |
PGPASSWORD=g@l3tte psql -d galette_tests -a -f galette-core/galette/install/scripts/pgsql.sql -U galette_tests -h localhost
- name: Unit tests
run: |
cd galette-core/galette/plugins/plugin-oauth2
../../vendor/bin/phpunit --test-suffix=.php --bootstrap tests/TestsBootstrap.php --no-coverage --process-isolation tests/GaletteOAuth2/
15 changes: 13 additions & 2 deletions lib/GaletteOAuth2/Authorization/UserHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,17 @@ public static function getUserData(Container $container, int $id, array $options
}
$groups = implode(',', $groups);

$phone = '';
if ($member->phone) {
$phone = $member->phone;
}
if ($member->gsm) {
if ($phone) {
$phone .= '/';
}
$phone .= $member->gsm;
}

return [
'id' => $member->id,
'identifier' => $member->id, //nextcloud
Expand All @@ -199,7 +210,7 @@ public static function getUserData(Container $container, int $id, array $options
'country' => $member->country,
'zip' => $member->zipcode,
'city' => $member->town,
'phone' => $member->phone . '/' . $member->gsm,
'phone' => $phone,

'status' => $member->status,
'state' => $etat_adhesion ? 'true' : 'false',
Expand Down Expand Up @@ -234,7 +245,7 @@ public static function mergeOptions(Config $config, $client_id, array $oauth_sco
// 'email' => 'uuuu@ik.me', 'emailVerified' => NULL, 'phone' => NULL,
// 'address' => NULL, 'country' => NULL, 'region' => NULL, 'city' => NULL, 'zip' => NULL

private static function stripAccents(string $str): string
public static function stripAccents(string $str): string
{
//FIXME: there is probably a better way to go.
//try with something like transliterator_transliterate("Any-Latin; Latin-ASCII; [^a-zA-Z0-9\.\ -_] Remove;", $str);
Expand Down
88 changes: 88 additions & 0 deletions tests/GaletteOAuth2/Authorization/tests/units/UserHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

/**
* Copyright © 2021-2024 The Galette Team
*
* This file is part of Galette OAuth2 plugin (https://galette-community.github.io/plugin-oauth2/).
*
* Galette is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Galette is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Galette OAuth2 plugin. If not, see <http://www.gnu.org/licenses/>.
*/

namespace GaletteOauth2\Authorization\tests\units;

use Galette\GaletteTestCase;

/**
* UserHelper tests
*
* @author Johan Cwiklinski <johan@x-tnd.be>
*/
class UserHelper extends GaletteTestCase
{
protected int $seed = 20230324120838;

/**
* Test stripAccents
*
* @return void
*/
public function testStripAccents(): void
{
/** @var \Galette\Core\Plugins */
global $plugins;

$str = "éè";
$this->assertSame('ee', \GaletteOAuth2\Authorization\UserHelper::stripAccents($str));
}

/**
* Test getUserData
*
* @return void
*/
public function testGetUserData(): void
{
global $container;

$this->initStatus();
$adh1 = $this->getMemberOne();
$user_data = \GaletteOAuth2\Authorization\UserHelper::getUserData(
$container,
$adh1->id,
[]
);

$this->assertSame(
[
'id' => $adh1->id,
'identifier' => $adh1->id,
'displayName' => $adh1->sname,
'username' => 'r.durand',
'userName' => 'r.durand',
'name' => 'r.durand',
'email' => $adh1->email,
'mail' => $adh1->email,
'language' => $adh1->language,
'country' => $adh1->country,
'zip' => $adh1->zipcode,
'city' => $adh1->town,
'phone' => $adh1->phone,
'status' => $adh1->status,
'state' => 'false',
'groups' => 'non-member'
],
$user_data
);
}
}
31 changes: 31 additions & 0 deletions tests/TestsBootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/**
* Copyright © 2021-2024 The Galette Team
*
* This file is part of Galette OAuth2 plugin (https://galette-community.github.io/plugin-oauth2/).
*
* Galette is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Galette is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Galette OAuth2 plugin. If not, see <http://www.gnu.org/licenses/>.
*/

/**
* Bootstrap tests file for OAuth2 plugin
*
* @author Johan Cwiklinski <johan@x-tnd.be>
*/

define('GALETTE_PLUGINS_PATH', __DIR__ . '/../../');
$basepath = '../../../galette/';

include_once '../../../tests/TestsBootstrap.php';

0 comments on commit 654a0b5

Please sign in to comment.