Skip to content

Commit b956951

Browse files
committed
Merge branch 'master' into dev/4.0
2 parents ddd6755 + 7a76515 commit b956951

File tree

6 files changed

+370
-14
lines changed

6 files changed

+370
-14
lines changed

ext.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ class ext extends \phpbb\extension\base
1919
const DEFAULT_PHPBB_MIN = '3.3.0';
2020
const DEFAULT_PHPBB_MAX = '4.0.0@dev';
2121

22+
const REQUIRE_PHPBB_MIN = '4.0.0-dev';
23+
const REQUIRE_PHP = 80100;
24+
2225
/**
2326
* @var array An array of installation error messages
2427
*/
@@ -40,26 +43,28 @@ public function is_enableable()
4043
}
4144

4245
/**
43-
* Check phpBB 3.2.3 minimum requirement.
46+
* Check phpBB requirements.
4447
*
48+
* @param string $phpBB_version
4549
* @return void
4650
*/
47-
protected function phpbb_requirement()
51+
protected function phpbb_requirement($phpBB_version = PHPBB_VERSION)
4852
{
49-
if (phpbb_version_compare(PHPBB_VERSION, '4.0.0-dev', '<'))
53+
if (phpbb_version_compare($phpBB_version, self::REQUIRE_PHPBB_MIN, '<'))
5054
{
5155
$this->errors[] = 'PHPBB_VERSION_ERROR';
5256
}
5357
}
5458

5559
/**
56-
* Check PHP 8.1 minimum requirement.
60+
* Check PHP minimum requirement.
5761
*
62+
* @param int $php_version
5863
* @return void
5964
*/
60-
protected function php_requirement()
65+
protected function php_requirement($php_version = PHP_VERSION_ID)
6166
{
62-
if (PHP_VERSION_ID < 80100)
67+
if ($php_version < self::REQUIRE_PHP)
6368
{
6469
$this->errors[] = 'PHP_VERSION_ERROR';
6570
}
@@ -68,11 +73,12 @@ protected function php_requirement()
6873
/**
6974
* Check PHP ZipArchive binary requirement.
7075
*
76+
* @param string $zip_class
7177
* @return void
7278
*/
73-
protected function ziparchive_exists()
79+
protected function ziparchive_exists($zip_class = 'ZipArchive')
7480
{
75-
if (!class_exists('ZipArchive'))
81+
if (!class_exists($zip_class, false))
7682
{
7783
$this->errors[] = 'NO_ZIPARCHIVE_ERROR';
7884
}

skeleton/notification/type/sample.php.twig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{% set is_phpbb_pre_32 = skeleton_version_compare(REQUIREMENTS.phpbb_version_max, "3.2", "<") %}
12
<?php
23
/**
34
*
@@ -157,7 +158,7 @@ class sample extends \phpbb\notification\type\base
157158
*
158159
* @param array $data The type specific data
159160
* @param array $pre_create_data Data from pre_create_insert_array()
160-
{% if skeleton_version_compare(REQUIREMENTS.phpbb_version_max, "3.2", "<") %}{# for phpBB 3.1.x only #}
161+
{% if is_phpbb_pre_32 %}{# for phpBB 3.1.x only #}
161162
*
162163
* @return array Array of data ready to be inserted into the database
163164
{% endif %}{# for phpBB >= 3.2.x #}
@@ -166,7 +167,7 @@ class sample extends \phpbb\notification\type\base
166167
{
167168
$this->set_data('{{ EXTENSION.extension_name|lower }}_sample_name', $data['{{ EXTENSION.extension_name|lower }}_sample_name']);
168169
169-
{% if skeleton_version_compare(REQUIREMENTS.phpbb_version_max, "3.2", "<") %}{# for phpBB 3.1.x only #}
170+
{% if is_phpbb_pre_32 %}{# for phpBB 3.1.x only #}
170171
return parent::create_insert_array($data, $pre_create_data);
171172
{% else %}{# for phpBB >= 3.2.x #}
172173
parent::create_insert_array($data, $pre_create_data);

skeleton/phpunit.xml.dist.twig

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{% if skeleton_version_compare(REQUIREMENTS.phpbb_version_max, "4.0", "<") %}
2+
{% set is_phpbb_pre_33 = skeleton_version_compare(REQUIREMENTS.phpbb_version_max, "3.3", "<") %}
23
<?xml version="1.0" encoding="UTF-8"?>
34

45
<phpunit backupGlobals="true"
@@ -9,7 +10,7 @@
910
convertWarningsToExceptions="true"
1011
processIsolation="false"
1112
stopOnFailure="false"
12-
{% if skeleton_version_compare(REQUIREMENTS.phpbb_version_max, "3.3", "<") %}
13+
{% if is_phpbb_pre_33 %}
1314
syntaxCheck="false"
1415
{% endif %}
1516
verbose="true"
@@ -21,12 +22,12 @@
2122
<exclude>./tests/functional</exclude>
2223
</testsuite>
2324
<testsuite name="Extension Functional Tests">
24-
<directory suffix="_test.php"{% if skeleton_version_compare(REQUIREMENTS.phpbb_version_max, "3.3", "<") %} phpVersion="5.3.19" phpVersionOperator=">="{% endif %}>./tests/functional/</directory>
25+
<directory suffix="_test.php"{% if is_phpbb_pre_33 %} phpVersion="5.3.19" phpVersionOperator=">="{% endif %}>./tests/functional/</directory>
2526
</testsuite>
2627
</testsuites>
2728

2829
<filter>
29-
{% if skeleton_version_compare(REQUIREMENTS.phpbb_version_max, "3.3", "<") %}
30+
{% if is_phpbb_pre_33 %}
3031
<blacklist>
3132
<directory>./tests/</directory>
3233
</blacklist>

tests/ext_test.php

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
/**
3+
*
4+
* This file is part of the phpBB Forum Software package.
5+
*
6+
* @copyright (c) phpBB Limited <https://www.phpbb.com>
7+
* @license GNU General Public License, version 2 (GPL-2.0)
8+
*
9+
* For full copyright and license information, please see
10+
* the docs/CREDITS.txt file.
11+
*
12+
*/
13+
14+
namespace phpbb\skeleton\tests;
15+
16+
use phpbb\db\migrator;
17+
use phpbb\finder;
18+
use phpbb\language\language;
19+
use phpbb\skeleton\ext;
20+
use Symfony\Component\DependencyInjection\ContainerInterface;
21+
22+
class ext_test extends \phpbb_test_case
23+
{
24+
protected $ext;
25+
26+
protected function setUp(): void
27+
{
28+
$this->ext = new ext(
29+
$this->createMock(ContainerInterface::class),
30+
$this->createMock(finder::class),
31+
$this->createMock(migrator::class),
32+
'phpbb/skeleton',
33+
''
34+
);
35+
}
36+
37+
public function test_ext_is_instance_of_base()
38+
{
39+
$this->assertInstanceOf(ext::class, $this->ext);
40+
}
41+
42+
public function test_is_enableable_true()
43+
{
44+
$ext = $this->getMockBuilder(ext::class)
45+
->disableOriginalConstructor()
46+
->setMethods(['ziparchive_exists', 'phpbb_requirement', 'php_requirement'])
47+
->getMock();
48+
49+
$ext->method('ziparchive_exists')->willReturn(null);
50+
$ext->method('phpbb_requirement')->willReturn(null);
51+
$ext->method('php_requirement')->willReturn(null);
52+
53+
$this->setExtErrors($ext, []);
54+
$this->assertTrue($ext->is_enableable());
55+
}
56+
57+
public function test_is_enableable_with_errors()
58+
{
59+
$ext = $this->getMockBuilder(ext::class)
60+
->disableOriginalConstructor()
61+
->setMethods(['ziparchive_exists', 'phpbb_requirement', 'php_requirement', 'enable_failed'])
62+
->getMock();
63+
64+
$ext->method('ziparchive_exists')->willReturnCallback(function () use ($ext) {
65+
$this->appendExtError($ext, 'NO_ZIPARCHIVE_ERROR');
66+
});
67+
$ext->method('phpbb_requirement')->willReturn(null);
68+
$ext->method('php_requirement')->willReturn(null);
69+
$ext->method('enable_failed')->willReturn(['NO_ZIPARCHIVE_ERROR']);
70+
71+
$this->setExtErrors($ext, ['NO_ZIPARCHIVE_ERROR']);
72+
$this->assertEquals(['NO_ZIPARCHIVE_ERROR'], $ext->is_enableable());
73+
}
74+
75+
public function test_enable_failed_returns_expected()
76+
{
77+
$ext = $this->getMockBuilder(ext::class)
78+
->disableOriginalConstructor()
79+
->getMock();
80+
81+
$this->setExtErrors($ext, ['SOME_ERROR']);
82+
83+
$languageMock = $this->createMock(language::class);
84+
$languageMock->method('add_lang')->willReturn(null);
85+
$languageMock->method('lang')->willReturnCallback(function ($msg) {
86+
return "LANG: $msg";
87+
});
88+
89+
$containerMock = $this->createMock(ContainerInterface::class);
90+
$containerMock->method('get')->with('language')->willReturn($languageMock);
91+
92+
$this->setProperty($ext, 'container', $containerMock);
93+
94+
$method = (new \ReflectionClass($ext))->getMethod('enable_failed');
95+
$method->setAccessible(true);
96+
97+
$this->assertEquals(['LANG: SOME_ERROR'], $method->invoke($ext));
98+
}
99+
100+
public function test_phpbb_requirement_min_error()
101+
{
102+
$this->setExtErrors($this->ext, []);
103+
$this->invokeProtectedMethod($this->ext, 'phpbb_requirement', ['3.2.2']);
104+
$this->assertContains('PHPBB_VERSION_MIN_ERROR', $this->getExtErrors($this->ext));
105+
}
106+
107+
public function test_phpbb_requirement_max_error()
108+
{
109+
$this->setExtErrors($this->ext, []);
110+
$this->invokeProtectedMethod($this->ext, 'phpbb_requirement', ['4.0.0-dev']);
111+
$this->assertContains('PHPBB_VERSION_MAX_ERROR', $this->getExtErrors($this->ext));
112+
}
113+
114+
public function test_php_requirement_error()
115+
{
116+
$this->setExtErrors($this->ext, []);
117+
$this->invokeProtectedMethod($this->ext, 'php_requirement', [50500]);
118+
$this->assertContains('PHP_VERSION_ERROR', $this->getExtErrors($this->ext));
119+
}
120+
121+
public function test_ziparchive_exists_error()
122+
{
123+
$this->setExtErrors($this->ext, []);
124+
$this->invokeProtectedMethod($this->ext, 'ziparchive_exists', ['NotZipArchive']);
125+
$this->assertContains('NO_ZIPARCHIVE_ERROR', $this->getExtErrors($this->ext));
126+
}
127+
128+
// --- Helpers ---
129+
130+
protected function invokeProtectedMethod($object, string $methodName, array $args = [])
131+
{
132+
$method = (new \ReflectionClass($object))->getMethod($methodName);
133+
$method->setAccessible(true);
134+
return $method->invokeArgs($object, $args);
135+
}
136+
137+
protected function getExtErrors($ext): array
138+
{
139+
$prop = (new \ReflectionClass($ext))->getProperty('errors');
140+
$prop->setAccessible(true);
141+
return $prop->getValue($ext);
142+
}
143+
144+
protected function setExtErrors($ext, array $errors): void
145+
{
146+
$prop = (new \ReflectionClass($ext))->getProperty('errors');
147+
$prop->setAccessible(true);
148+
$prop->setValue($ext, $errors);
149+
}
150+
151+
protected function appendExtError($ext, string $error): void
152+
{
153+
$errors = $this->getExtErrors($ext);
154+
$errors[] = $error;
155+
$this->setExtErrors($ext, $errors);
156+
}
157+
158+
protected function setProperty($object, string $property, $value): void
159+
{
160+
$prop = (new \ReflectionClass($object))->getProperty($property);
161+
$prop->setAccessible(true);
162+
$prop->setValue($object, $value);
163+
}
164+
}

0 commit comments

Comments
 (0)