Skip to content

Commit d7b24f7

Browse files
authored
Merge pull request #7 from WikiTeq/WIK-1545_update-tests
Fix tests, More precise issue matching regular expression WIK-1545
2 parents 6bbcb06 + f3eea74 commit d7b24f7

File tree

8 files changed

+199
-70
lines changed

8 files changed

+199
-70
lines changed

.github/workflows/ci.yml

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
name: Continuous Integration
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
env:
10+
EXTNAME: SummaryToJiraComment
11+
MW_INSTALL_PATH: ${{ github.workspace }}
12+
13+
jobs:
14+
style-php:
15+
name: Code Style (PHP)
16+
runs-on: ubuntu-latest
17+
steps:
18+
- name: Setup PHP
19+
uses: shivammathur/setup-php@v2
20+
with:
21+
php-version: '8.1'
22+
extensions: mbstring, intl
23+
coverage: none
24+
tools: composer, phpcs, phplint
25+
- name: Setup Extension
26+
uses: actions/checkout@v3
27+
- name: Setup Composer
28+
run: composer update
29+
- name: Lint
30+
run: phplint -w --exclude=vendor
31+
- name: PHP Code Sniffer
32+
run: vendor/bin/phpcs -sp --standard=.phpcs.xml
33+
34+
security:
35+
name: Static Analysis
36+
runs-on: ubuntu-latest
37+
strategy:
38+
fail-fast: false
39+
matrix:
40+
php: [ '8.1' ]
41+
mediawiki: [ REL1_39, REL1_41, master ]
42+
steps:
43+
- name: Setup PHP
44+
uses: shivammathur/setup-php@v2
45+
with:
46+
php-version: ${{ matrix.php }}
47+
extensions: mbstring, intl, ast
48+
coverage: none
49+
tools: composer
50+
- name: Setup MediaWiki
51+
uses: actions/checkout@v3
52+
with:
53+
repository: wikimedia/mediawiki
54+
ref: ${{ matrix.mediawiki }}
55+
- name: Setup Extension
56+
uses: actions/checkout@v3
57+
with:
58+
path: extensions/${{ env.EXTNAME }}
59+
- name: Setup Composer
60+
run: |
61+
echo '{"extra":{"merge-plugin":{"include":["extensions/*/composer.json","skins/*/composer.json"]}}}' > composer.local.json
62+
composer update
63+
composer update
64+
- name: Phan
65+
run: ./vendor/bin/phan -d extensions/${{ env.EXTNAME }} --minimum-target-php-version=7.4 --long-progress-bar
66+
67+
test:
68+
name: PHPUnit
69+
runs-on: ubuntu-latest
70+
env:
71+
MW_INSTALL_PATH: ${{ github.workspace }}/mediawiki
72+
strategy:
73+
fail-fast: false
74+
matrix:
75+
php: ["8.1"]
76+
mediawiki: [REL1_39]
77+
steps:
78+
- name: Setup Extension
79+
uses: actions/checkout@v3
80+
- name: Mediawiki PHPUnit
81+
uses: wikiteq/mediawiki-phpunit-action@master
82+
with:
83+
type: extension
84+
php: ${{ matrix.php }}
85+
mwbranch: ${{ matrix.mediawiki }}
86+
extension: ${{ env.EXTNAME }}

.phan/config.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<?php
22

3-
return require __DIR__ . '/../vendor/mediawiki/mediawiki-phan-config/src/config.php';
3+
// use phan config shipped with mediawiki core
4+
return require __DIR__ . '/../../../vendor/mediawiki/mediawiki-phan-config/src/config.php';

composer.json

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"require-dev": {
3-
"mediawiki/mediawiki-codesniffer": "41.0.0",
4-
"mediawiki/mediawiki-phan-config": "0.12.1",
5-
"mediawiki/minus-x": "1.1.1",
6-
"php-parallel-lint/php-console-highlighter": "1.0.0",
7-
"php-parallel-lint/php-parallel-lint": "1.3.2"
3+
"mediawiki/mediawiki-codesniffer": "38.0.0 || 42.0.0 || 43.0.0",
4+
"mediawiki/mediawiki-phan-config": "0.12.1 || 0.14.0",
5+
"mediawiki/minus-x": "1.1.3",
6+
"php-parallel-lint/php-console-highlighter": "0.5 || 1.0.0",
7+
"php-parallel-lint/php-parallel-lint": "1.3.1 || 1.3.2 || 1.4.0"
88
},
99
"scripts": {
1010
"test": [
@@ -18,5 +18,10 @@
1818
],
1919
"phan": "phan -d . --long-progress-bar",
2020
"phpcs": "phpcs -sp --cache"
21+
},
22+
"config": {
23+
"allow-plugins": {
24+
"dealerdirect/phpcodesniffer-composer-installer": true
25+
}
2126
}
22-
}
27+
}

extension.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@
5050
}
5151
},
5252
"manifest_version": 2
53-
}
53+
}

src/Hooks.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828

2929
class Hooks {
3030

31+
/**
32+
* @var MultiHttpClient
33+
*/
3134
public static MultiHttpClient $httpClient;
3235

3336
/**
@@ -71,9 +74,9 @@ public static function onPageSaveComplete(
7174
* @param string $summary
7275
* @return array
7376
*/
74-
private static function getJiraIssueKeys( $summary ): array {
77+
public static function getJiraIssueKeys( $summary ): array {
7578
$issueKeys = [];
76-
$issueKeyRegex = '/([A-Z0-9]+-[0-9]+)/';
79+
$issueKeyRegex = '/\b([A-Z][A-Z0-9]*-[0-9]+)\b/';
7780
$matches = [];
7881
preg_match_all( $issueKeyRegex, $summary, $matches );
7982
if ( isset( $matches[1] ) ) {
@@ -90,8 +93,8 @@ private static function getJiraIssueKeys( $summary ): array {
9093
* @param string $summary
9194
* @return bool
9295
*/
93-
private static function sendToJira( $config, $issueKey, $summary ): bool {
94-
list( $instance, $token, $email ) = $config;
96+
public static function sendToJira( $config, $issueKey, $summary ): bool {
97+
[ $instance, $token, $email ] = $config;
9598
$hash = base64_encode( $email . ':' . $token );
9699

97100
self::$httpClient = new MultiHttpClient( [ 'maxRetries' => 3 ] );
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace MediaWiki\Extension\SummaryToJiraComment\Tests;
4+
5+
use MediaWiki\Extension\SummaryToJiraComment\Hooks;
6+
use MediaWiki\MediaWikiServices;
7+
use MediaWiki\Revision\RevisionRecord;
8+
use MediaWiki\Storage\EditResult;
9+
use User;
10+
use WikiPage;
11+
12+
/**
13+
* @coversDefaultClass \MediaWiki\Extension\SummaryToJiraComment\Hooks
14+
*/
15+
class HooksIntegrationTest extends \MediaWikiIntegrationTestCase {
16+
17+
protected function setUp(): void {
18+
parent::setUp();
19+
$this->overrideConfigValue( 'SummaryToJiraCommentInstance', 'jira.example.com' );
20+
$this->overrideConfigValue( 'SummaryToJiraCommentToken', '0xCAFEBEEF' );
21+
$this->overrideConfigValue( 'SummaryToJiraCommentEmail', 'test@example.com' );
22+
}
23+
24+
/**
25+
* @covers ::onPageSaveComplete
26+
*/
27+
public function testOnPageSaveComplete() {
28+
$titleFactory = MediaWikiServices::getInstance()->getTitleFactory();
29+
$wikiPage = $this->createConfiguredMock( WikiPage::class,
30+
// We need a real title for getting a diff link
31+
[ 'getTitle' => $titleFactory->newFromText( 'Test page' ) ]
32+
);
33+
$user = $this->createMock( User::class );
34+
$summary = 'Test summary';
35+
$flags = 0;
36+
37+
$revisionRecord = $this->createMock( RevisionRecord::class );
38+
$editResult = $this->createMock( EditResult::class );
39+
$result = Hooks::onPageSaveComplete( $wikiPage, $user, $summary, $flags, $revisionRecord, $editResult );
40+
41+
$this->assertTrue( $result );
42+
}
43+
}

tests/phpunit/unit/HooksTest.php

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace MediaWiki\Extension\SummaryToJiraComment\Tests;
4+
5+
use MediaWiki\Extension\SummaryToJiraComment\Hooks;
6+
use MultiHttpClient;
7+
8+
/**
9+
* @coversDefaultClass \MediaWiki\Extension\SummaryToJiraComment\Hooks
10+
*/
11+
class HooksUnitTest extends \MediaWikiUnitTestCase {
12+
13+
/**
14+
* @covers ::sendToJira
15+
*/
16+
public function testSendToJira() {
17+
$config = [
18+
'https://jira.example.com',
19+
'token',
20+
'test@example.com'
21+
];
22+
$issueKey = 'TEST-1';
23+
$summary = 'Test summary';
24+
25+
$httpClient = $this->createMock( MultiHttpClient::class );
26+
// body should be any non-empty array
27+
$response = [ 'code' => 201, 'body' => [ 'foo' => 'bar' ] ];
28+
$httpClient->method( 'run' )->willReturn( $response );
29+
30+
Hooks::$httpClient = $httpClient;
31+
$result = Hooks::sendToJira( $config, $issueKey, $summary );
32+
33+
$this->assertTrue( $result );
34+
}
35+
36+
/**
37+
* @covers ::getJiraIssueKeys
38+
*/
39+
public function testGetJiraIssueKeys() {
40+
$summary = 'TEST-1 Test summary TEST1-1 11-22 test-11 R2-D2 C-3PO BB-8 THX 1138 TEST1T2-3 ' .
41+
' #TEST1-7 A0-3 WW-II EMDASH—1 ENDASH–2 DOUBLEDASH--3 [BRACKET-1] (PAREN-2)';
42+
$issueKeys = Hooks::getJiraIssueKeys( $summary );
43+
44+
$this->assertSame(
45+
[ 'TEST-1', 'TEST1-1', 'BB-8', 'TEST1T2-3', 'TEST1-7', 'A0-3', 'BRACKET-1', 'PAREN-2' ],
46+
$issueKeys
47+
);
48+
}
49+
}

0 commit comments

Comments
 (0)