Skip to content

Commit 5c0ec66

Browse files
authored
Feature/unit tests (#79)
2 parents 64ae065 + daf20b8 commit 5c0ec66

47 files changed

Lines changed: 6148 additions & 571 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/tests.yml

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
workflow_dispatch:
9+
10+
jobs:
11+
phpunit-tests:
12+
name: PHPUnit Tests
13+
runs-on: ubuntu-latest
14+
15+
services:
16+
mysql:
17+
image: mysql:8.0
18+
env:
19+
MYSQL_ROOT_PASSWORD: root
20+
MYSQL_DATABASE: ojs_ci
21+
ports:
22+
- 3306:3306
23+
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
24+
25+
steps:
26+
- name: Checkout OJS
27+
uses: actions/checkout@v4
28+
with:
29+
repository: pkp/ojs
30+
ref: stable-3_5_0
31+
path: ojs
32+
submodules: recursive
33+
34+
- name: Setup PHP
35+
uses: shivammathur/setup-php@v2
36+
with:
37+
php-version: '8.2'
38+
extensions: mbstring, xml, pdo_mysql, intl, zip, bcmath, curl, gd
39+
coverage: xdebug
40+
41+
- name: Install PKP dependencies
42+
working-directory: ojs/lib/pkp
43+
run: composer install --no-interaction --no-progress --optimize-autoloader
44+
45+
- name: Checkout plugin
46+
uses: actions/checkout@v4
47+
with:
48+
path: ojs/plugins/generic/codecheck
49+
50+
- name: Install plugin dependencies
51+
working-directory: ojs/plugins/generic/codecheck
52+
run: composer install --no-interaction || true
53+
54+
- name: Create .env file
55+
working-directory: ojs/plugins/generic/codecheck
56+
run: |
57+
cat > .env << EOF
58+
GITHUB_TOKEN=${{ secrets.CODECHECK_GITHUB_TOKEN }}
59+
GITHUB_OWNER=${{ secrets.CODECHECK_GITHUB_OWNER }}
60+
GITHUB_REPO=${{ secrets.CODECHECK_GITHUB_REPO }}
61+
EOF
62+
63+
- name: Setup OJS database
64+
working-directory: ojs
65+
run: |
66+
mysql -h 127.0.0.1 -u root -proot -e "CREATE DATABASE IF NOT EXISTS ojs_ci;"
67+
cp config.TEMPLATE.inc.php config.inc.php
68+
sed -i 's/username = .*/username = root/' config.inc.php
69+
sed -i 's/password = .*/password = root/' config.inc.php
70+
sed -i 's/name = .*/name = ojs_ci/' config.inc.php
71+
sed -i 's/host = .*/host = 127.0.0.1/' config.inc.php
72+
73+
- name: Create MySQL socket symlink
74+
run: |
75+
sudo mkdir -p /tmp
76+
sudo ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock || true
77+
78+
- name: Run PHPUnit tests
79+
working-directory: ojs/plugins/generic/codecheck/tests
80+
run: sh runTests.sh
81+
82+
- name: Run PHPUnit tests with coverage
83+
working-directory: ojs/plugins/generic/codecheck/tests
84+
run: sh runTests.sh --coverage-report=true
85+
86+
- name: Upload coverage report
87+
if: success()
88+
uses: actions/upload-artifact@v4
89+
with:
90+
name: phpunit-coverage
91+
path: ojs/plugins/generic/codecheck/tests/results/
92+
if-no-files-found: ignore
93+
94+
- name: Upload test logs
95+
if: failure()
96+
uses: actions/upload-artifact@v4
97+
with:
98+
name: phpunit-logs
99+
path: |
100+
ojs/plugins/generic/codecheck/tests/*.log
101+
ojs/plugins/generic/codecheck/tests/*.xml
102+
if-no-files-found: ignore
103+
104+
component-tests:
105+
name: Cypress Component Tests
106+
runs-on: ubuntu-latest
107+
108+
steps:
109+
- name: Checkout code
110+
uses: actions/checkout@v4
111+
112+
- name: Setup Node.js
113+
uses: actions/setup-node@v4
114+
with:
115+
node-version: '18'
116+
cache: 'npm'
117+
118+
- name: Install dependencies
119+
run: npm ci
120+
121+
- name: Run component tests
122+
run: npm run test:component
123+
124+
- name: Upload screenshots
125+
if: failure()
126+
uses: actions/upload-artifact@v4
127+
with:
128+
name: cypress-screenshots
129+
path: cypress/screenshots
130+
if-no-files-found: ignore

.gitignore

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
# CakePHP 3
55

6-
/vendor/*
6+
/vendor/
77
/config/app.php
88

99
/tmp/cache/models/*
@@ -20,8 +20,6 @@
2020
/logs/*
2121
!/logs/empty
2222

23-
.phpunit.result.cache
24-
2523
# CakePHP 2
2624

2725
/app/tmp/*
@@ -37,4 +35,9 @@
3735
node_modules/
3836

3937
# npm release build
40-
public/
38+
public/
39+
40+
# test results
41+
tests/.phpunit.result.cache
42+
tests/results/
43+
.phpunit.cache/

CodecheckPlugin.php

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -41,37 +41,6 @@ public function register($category, $path, $mainContextId = null): bool
4141
// Add hook for the Template Manager
4242
Hook::add('TemplateManager::display', $this->callbackTemplateManagerDisplay(...));
4343

44-
$metadataHandler = new CodecheckMetadataHandler($this);
45-
Hook::add('LoadHandler', function($hookName, $args) use ($metadataHandler) {
46-
$page = $args[0];
47-
$op = $args[1];
48-
49-
if ($page === 'codecheck') {
50-
$request = Application::get()->getRequest();
51-
$submissionId = $request->getUserVar('submissionId');
52-
53-
if ($op === 'metadata' && $request->isGet()) {
54-
$result = $metadataHandler->getMetadata($request, $submissionId);
55-
header('Content-Type: application/json');
56-
echo json_encode($result);
57-
exit;
58-
} elseif ($op === 'metadata' && $request->isPost()) {
59-
$result = $metadataHandler->saveMetadata($request, $submissionId);
60-
header('Content-Type: application/json');
61-
echo json_encode($result);
62-
exit;
63-
} elseif ($op === 'yaml') {
64-
$result = $metadataHandler->generateYaml($request, $submissionId);
65-
header('Content-Type: application/json');
66-
echo json_encode($result);
67-
exit;
68-
}
69-
70-
}
71-
72-
return false;
73-
});
74-
7544
// Wizard fields schema
7645
$codecheckSchema = new Schema();
7746
Hook::add('Schema::get::publication', function($hookName, $args) use ($codecheckSchema) {
@@ -283,8 +252,8 @@ public function setEnabled($enabled, $contextId = null)
283252
$result = parent::setEnabled($enabled, $contextId);
284253

285254
if ($enabled) {
286-
$migration = new CodecheckSchemaMigration();
287-
$migration->up();
255+
$migration = new CodecheckSchemaMigration();
256+
$migration->up();
288257
}
289258

290259
return $result;

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
[![repo status](https://www.repostatus.org/badges/latest/wip.svg)](https://www.repostatus.org/#wip)
66
[![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
77
[![Contributions - welcome](https://img.shields.io/badge/Contributions-welcome-blueviolet)](https://github.com/codecheckers/ojs-codecheck/blob/main/CONTRIBUTING.md)
8+
[![Component Tests](https://github.com/codecheckers/ojs-codecheck/actions/workflows/component-tests.yml/badge.svg?branch=main)](https://github.com/codecheckers/ojs-codecheck/actions/workflows/component-tests.yml)
89
<br />
910

1011
An [OJS Plugin](https://docs.pkp.sfu.ca/dev/plugin-guide/en/) to streamline codechecking of submissions and display of [CODECHECK](https://codecheck.org.uk/) certificates.
@@ -119,6 +120,7 @@ The plugin tracks CODECHECK progress through a status system displayed in the re
119120
### Status Implementation
120121

121122
The status is determined in `CodecheckReviewDisplay.vue` using the following logic:
123+
122124
```javascript
123125
function getStatus() {
124126
if (metadata.value.certificate && metadata.value.checkTime) {
@@ -144,18 +146,21 @@ function getStatus() {
144146
This plugin uses **Vite** for building Vue.js components.
145147

146148
#### Install dependencies
149+
147150
```bash
148151
npm install
149152
```
150153

151154
#### Build for production
155+
152156
```bash
153157
npm run build
154158
```
155159

156160
This compiles Vue components and JavaScript into the `public/` directory.
157161

158162
#### Watch mode (development)
163+
159164
```bash
160165
npm run dev
161166
```
@@ -242,6 +247,7 @@ codecheck/
242247
├── locale/* # Internationalization (language localization strings)
243248
├── package-lock.json
244249
├── package.json
250+
├── package-plugin.sh # Shell script, that makes packaging this plugin reproducible
245251
├── public/build/* # NPM realese build files
246252
├── resources/js/* # The Vue.js Components
247253
├── schema.xml # CODECHECK metadata table schema file
@@ -288,6 +294,68 @@ private function yourFunction(): void
288294
}
289295
```
290296
297+
## Running Tests
298+
299+
The plugin includes comprehensive test coverage for both backend PHP code and frontend Vue components.
300+
301+
### PHP Unit Tests
302+
303+
**Note:** Some tests require the full OJS environment (e.g.: database, facades, translations).
304+
They run successfully in [**Option 2: Docker/CI Environment**](#option-2-dockerci-environment) but are skipped in [**Option 1: Local Testing**](#option-1-local-testing).
305+
306+
#### Option 1: Local Testing
307+
308+
From the plugins directory:
309+
310+
1. Navigate to the `tests/` directory:
311+
```bash
312+
cd plugins/generic/codecheck/tests/
313+
```
314+
2. Run the tests:
315+
- **without** a test coverage report:
316+
```bash
317+
sh runTests.sh
318+
```
319+
or
320+
```bash
321+
sh runTests.sh --coverage-report=false
322+
```
323+
- **with** a test coverage report:
324+
```bash
325+
sh runTests.sh --coverage-report=true
326+
```
327+
**Note:** You will find the test coverage in the `tests/results/index.html` file.
328+
329+
#### Option 2: Docker/CI Environment
330+
331+
From the root of the OJS application directory:
332+
333+
```bash
334+
lib/pkp/lib/vendor/phpunit/phpunit/phpunit -c lib/pkp/tests/phpunit.xml plugins/generic/codecheck/tests/
335+
```
336+
337+
If you want to visualize the test coverage, open the test coverage report which is located in the following file:
338+
339+
```bash
340+
lib/pkp/tests/results/index.html
341+
```
342+
343+
### Frontend Component Tests
344+
345+
[![Component Tests](https://github.com/codecheckers/ojs-codecheck/actions/workflows/component-tests.yml/badge.svg?branch=main)](https://github.com/codecheckers/ojs-codecheck/actions/workflows/component-tests.yml)
346+
347+
From the plugin directory:
348+
349+
```bash
350+
npm run test:component
351+
```
352+
353+
For interactive testing:
354+
355+
```bash
356+
npm run test:component:open
357+
```
358+
291359
## License
292360
293361
Copyright (c) 2025 CODECHECK Initiative

0 commit comments

Comments
 (0)