This repository has been archived by the owner on Nov 26, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: add basic tests * Apply fixes from StyleCI Co-authored-by: StyleCI Bot <[email protected]>
- Loading branch information
1 parent
9dde79e
commit ee4842e
Showing
9 changed files
with
234 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
name: FoF Spamblock | ||
|
||
on: [workflow_dispatch, push, pull_request] | ||
|
||
jobs: | ||
run: | ||
uses: flarum/framework/.github/workflows/REUSABLE_backend.yml@main | ||
with: | ||
enable_backend_testing: true | ||
|
||
backend_directory: . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ node_modules | |
vendor | ||
composer.lock | ||
js/dist | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fof/spamblock. | ||
* | ||
* Copyright (c) FriendsOfFlarum. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FoF\Spamblock\Tests\integration\api; | ||
|
||
use Carbon\Carbon; | ||
use Flarum\Group\Group; | ||
use Flarum\Post\CommentPost; | ||
use Flarum\Testing\integration\TestCase; | ||
|
||
class SpamblockTest extends TestCase | ||
{ | ||
protected function setup(): void | ||
{ | ||
parent::setup(); | ||
|
||
$this->extension('fof-spamblock'); | ||
|
||
$this->prepareDatabase([ | ||
'users' => [ | ||
['id' => 3, 'username' => 'a_moderator', 'email' => '[email protected]', 'is_email_confirmed' => 1], | ||
['id' => 4, 'username' => 'toby', 'email' => '[email protected]', 'is_email_confirmed' => 1], | ||
['id' => 5, 'username' => 'bad_user', 'email' => '[email protected]', 'is_email_confirmed' => 1], | ||
], | ||
'group_user' => [ | ||
['user_id' => 3, 'group_id' => Group::MODERATOR_ID], | ||
], | ||
'group_permission' => [ | ||
['group_id' => Group::MODERATOR_ID, 'permission' => 'user.spamblock'], | ||
], | ||
'discussions' => [ | ||
['id' => 2, 'title' => __CLASS__, 'created_at' => Carbon::now(), 'last_posted_at' => Carbon::now(), 'user_id' => 5, 'first_post_id' => 4, 'comment_count' => 2, 'last_post_id' => 5], | ||
], | ||
'posts' => [ | ||
['id' => 4, 'number' => 2, 'discussion_id' => 2, 'created_at' => Carbon::now(), 'user_id' => 5, 'type' => 'comment', 'content' => '<r>Some spammy content</r>'], | ||
['id' => 5, 'number' => 3, 'discussion_id' => 2, 'created_at' => Carbon::now(), 'user_id' => 4, 'type' => 'comment', 'content' => '<r>Some regular content</r>'], | ||
], | ||
]); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function moderator_cannot_spamblock_self() | ||
{ | ||
$response = $this->send( | ||
$this->request('POST', 'api/users/3/spamblock', [ | ||
'authenticatedAs' => 3, | ||
]) | ||
); | ||
|
||
$this->assertEquals(403, $response->getStatusCode()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function user_without_permissions_cannot_spamblock() | ||
{ | ||
$response = $this->send( | ||
$this->request('POST', 'api/users/3/spamblock', [ | ||
'authenticatedAs' => 4, | ||
]) | ||
); | ||
|
||
$this->assertEquals(403, $response->getStatusCode()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function moderator_can_spamblock_and_posts_are_hidden() | ||
{ | ||
$response = $this->send( | ||
$this->request('POST', 'api/users/5/spamblock', [ | ||
'authenticatedAs' => 3, | ||
]) | ||
); | ||
|
||
$this->assertEquals(204, $response->getStatusCode()); | ||
|
||
$response = $this->send( | ||
$this->request('GET', 'api/discussions/2', [ | ||
'authenticatedAs' => 3, | ||
]) | ||
); | ||
|
||
$this->assertEquals(200, $response->getStatusCode()); | ||
|
||
$response = json_decode($response->getBody(), true); | ||
|
||
$this->assertTrue($response['data']['attributes']['isHidden']); | ||
$this->assertNotNull($response['data']['attributes']['hiddenAt']); | ||
$this->assertNotNull(CommentPost::find(4)->hidden_at); | ||
$this->assertNull(CommentPost::find(5)->hidden_at); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function normal_user_cannot_see_spamblocked_posts() | ||
{ | ||
$response = $this->send( | ||
$this->request('POST', 'api/users/5/spamblock', [ | ||
'authenticatedAs' => 3, | ||
]) | ||
); | ||
|
||
$this->assertEquals(204, $response->getStatusCode()); | ||
|
||
$response = $this->send( | ||
$this->request('GET', 'api/discussions/2', [ | ||
'authenticatedAs' => 4, | ||
]) | ||
); | ||
|
||
$this->assertEquals(404, $response->getStatusCode()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fof/spamblock. | ||
* | ||
* Copyright (c) FriendsOfFlarum. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
use Flarum\Testing\integration\Setup\SetupScript; | ||
|
||
require __DIR__.'/../../vendor/autoload.php'; | ||
|
||
$setup = new SetupScript(); | ||
|
||
$setup->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" | ||
backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="true" | ||
stopOnFailure="false" | ||
> | ||
<coverage processUncoveredFiles="true"> | ||
<include> | ||
<directory suffix=".php">../src/</directory> | ||
</include> | ||
</coverage> | ||
<testsuites> | ||
<testsuite name="Flarum Integration Tests"> | ||
<directory suffix="Test.php">./integration</directory> | ||
<exclude>./integration/tmp</exclude> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" | ||
backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
> | ||
<coverage processUncoveredFiles="true"> | ||
<include> | ||
<directory suffix=".php">../src/</directory> | ||
</include> | ||
</coverage> | ||
<testsuites> | ||
<testsuite name="Flarum Unit Tests"> | ||
<directory suffix="Test.php">./unit</directory> | ||
</testsuite> | ||
</testsuites> | ||
<listeners> | ||
<listener class="\Mockery\Adapter\Phpunit\TestListener" /> | ||
</listeners> | ||
</phpunit> |
Empty file.