Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Friedman <[email protected]>
  • Loading branch information
iMattPro committed Jan 18, 2024
1 parent 11eaeaf commit 2e2205a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 82 deletions.
2 changes: 1 addition & 1 deletion controller/admin_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function add_prefix()
{
$this->trigger_message('TOPIC_PREFIXES_INPUT_EMPTY', E_USER_WARNING);
}

$prefix = $this->manager->add_prefix($tag, $this->forum_id);
$this->log($prefix['prefix_tag'], 'ACP_LOG_PREFIX_ADDED');
}
Expand Down
68 changes: 0 additions & 68 deletions tests/controller/add_fails_test.php

This file was deleted.

48 changes: 35 additions & 13 deletions tests/controller/add_prefix_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,25 @@ class add_prefix_test extends admin_controller_base
*/
public function data_add_prefix()
{
return array(
array(true, true),
array(true, false),
array(false, false),
);
return [
['topic_prefix1', true, true], // valid prefix, form is being submitted, form is valid
['topic_prefix2', true, false], // valid prefix, form is being submitted, form is invalid
['topic_prefix3', false, false], // valid prefix, form is not being submitted, form is invalid
['', true, true], // invalid prefix, form is being submitted, form is valid
['', true, false], // invalid prefix, form is being submitted, form is invalid
['', false, true], // invalid prefix, form is not being submitted, form is invalid
];
}

/**
* Test add_prefix()
*
* @dataProvider data_add_prefix
* @param $prefix_id
* @param $submit
* @param $valid_form
*/
public function test_add_prefix($submit, $valid_form)
public function test_add_prefix($prefix_id, $submit, $valid_form)
{
if ($submit)
{
Expand All @@ -52,20 +56,38 @@ public function test_add_prefix($submit, $valid_form)
if (!$valid_form)
{
// Throws E_WARNING in PHP 8.0+ and E_USER_WARNING in earlier versions
$exceptionName = version_compare(PHP_VERSION, '8.0', '<') ? \PHPUnit\Framework\Error\Error::class : \PHPUnit\Framework\Error\Warning::class;
$errno = version_compare(PHP_VERSION, '8.0', '<') ? E_USER_WARNING : E_WARNING;
$exceptionName = PHP_VERSION_ID < 80000 ? \PHPUnit\Framework\Error\Error::class : \PHPUnit\Framework\Error\Warning::class;
$errno = PHP_VERSION_ID < 80000 ? E_USER_WARNING : E_WARNING;
$this->expectExceptionMessage($this->language->lang('FORM_INVALID'));
$this->expectException($exceptionName);
$this->expectExceptionCode($errno);
}
else
{
$this->manager->expects(static::once())
$valid_prefix = $prefix_id !== '';

$this->request->expects(static::once())
->method('variable')
->willReturnMap([
['prefix_tag', '', true, \phpbb\request\request_interface::REQUEST, $prefix_id],
]);

if (!$valid_prefix) // Empty string prefixes should trigger error
{
// Throws E_WARNING in PHP 8.0+ and E_USER_WARNING in earlier versions
$exceptionName = PHP_VERSION_ID < 80000 ? \PHPUnit\Framework\Error\Error::class : \PHPUnit\Framework\Error\Warning::class;
$errno = PHP_VERSION_ID < 80000 ? E_USER_WARNING : E_WARNING;
$this->expectExceptionMessage($this->language->lang('TOPIC_PREFIXES_INPUT_EMPTY'));
$this->expectException($exceptionName);
$this->expectExceptionCode($errno);
}
$this->manager->expects($valid_prefix ? static::once() : static::never())
->method('add_prefix')
->willReturn(['prefix_tag' => 'topic_prefix']);
$this->log->expects(static::once())
->willReturn(['prefix_tag' => $prefix_id]);
$this->log->expects($valid_prefix ? static::once() : static::never())
->method('add')
->with('admin', static::anything(), static::anything(), 'ACP_LOG_PREFIX_ADDED', static::anything(), ['topic_prefix', 'Test Forum']);
$this->db->expects(static::once())
->with('admin', static::anything(), static::anything(), 'ACP_LOG_PREFIX_ADDED', static::anything(), [$prefix_id, 'Test Forum']);
$this->db->expects($valid_prefix ? static::once() : static::never())
->method('sql_fetchrow')
->willReturn(['forum_name' => 'Test Forum']);
}
Expand Down

0 comments on commit 2e2205a

Please sign in to comment.