Skip to content
This repository has been archived by the owner on Nov 26, 2023. It is now read-only.

Commit

Permalink
Add tests (#22)
Browse files Browse the repository at this point in the history
* chore: add basic tests

* Apply fixes from StyleCI

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
imorland and StyleCIBot authored Nov 2, 2022
1 parent 9dde79e commit ee4842e
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/backend.yml
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: .
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
vendor
composer.lock
js/dist
.phpunit.result.cache
25 changes: 25 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,30 @@
"flagrow": {
"discuss": "https://discuss.flarum.org/d/17772"
}
},
"scripts": {
"test": [
"@test:unit",
"@test:integration"
],
"test:unit": "phpunit -c tests/phpunit.unit.xml",
"test:integration": "phpunit -c tests/phpunit.integration.xml",
"test:setup": "@php tests/integration/setup.php"
},
"scripts-descriptions": {
"test": "Runs all tests.",
"test:unit": "Runs all unit tests.",
"test:integration": "Runs all integration tests.",
"test:setup": "Sets up a database for use with integration tests. Execute this only once."
},
"require-dev": {
"flarum/core": "*@dev",
"flarum/flags": "*@dev",
"flarum/suspend": "*@dev",
"flarum/testing": "^1.0.0",
"symfony/var-dumper": "*"
},
"config": {
"sort-packages": true
}
}
Empty file added tests/fixtures/.gitkeep
Empty file.
127 changes: 127 additions & 0 deletions tests/integration/api/SpamblockTest.php
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());
}
}
18 changes: 18 additions & 0 deletions tests/integration/setup.php
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();
25 changes: 25 additions & 0 deletions tests/phpunit.integration.xml
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>
27 changes: 27 additions & 0 deletions tests/phpunit.unit.xml
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 added tests/unit/.gitkeep
Empty file.

0 comments on commit ee4842e

Please sign in to comment.