Skip to content

Commit

Permalink
chore: add tests for basic identification of bad words
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Jun 12, 2024
1 parent c327690 commit f5129c3
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ node_modules
bower_components
js/dist
composer.lock
.phpunit.result.cache
26 changes: 22 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,36 @@
},
"flarum-cli": {
"modules": {
"githubActions": true
"githubActions": true,
"backendTesting": true
}
}
},
"require-dev": {
"flarum/phpstan": "*"
"flarum/phpstan": "*",
"flarum/testing": "^1.0.0"
},
"scripts": {
"analyse:phpstan": "phpstan analyse",
"clear-cache:phpstan": "phpstan clear-result-cache"
"clear-cache:phpstan": "phpstan clear-result-cache",
"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": {
"analyse:phpstan": "Run static analysis"
"analyse:phpstan": "Run static analysis",
"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."
},
"autoload-dev": {
"psr-4": {
"FoF\\Filter\\Tests\\": "tests/"
}
}
}
Empty file added tests/fixtures/.gitkeep
Empty file.
105 changes: 105 additions & 0 deletions tests/integration/api/CreatePostTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace FoF\Filter\Tests\integration\api;

use Flarum\Discussion\Discussion;
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
use Flarum\Testing\integration\TestCase;
use Illuminate\Support\Arr;

class CreatePostTest extends TestCase
{
use RetrievesAuthorizedUsers;

protected function setUp(): void
{
$this->extension('flarum-flags', 'flarum-approval', 'fof-filter');

$this->prepareDatabase([
'users' => [
$this->normalUser(),
],
'settings' => [
['key' => 'fof-filter.words', 'value' => 'wibble'.PHP_EOL.'wobble'.PHP_EOL],
['key' => 'fof-filter.censors', 'value' => '["\/(w|w\\.|w\\-|\u03c9|\u03c8|\u03a8)(i|i\\.|i\\-|!|\\||\\]\\[|]|1|\u222b|\u00cc|\u00cd|\u00ce|\u00cf|\u00ec|\u00ed|\u00ee|\u00ef)(b|b\\.|b\\-|8|\\|3|\u00df|\u0392|\u03b2)(b|b\\.|b\\-|8|\\|3|\u00df|\u0392|\u03b2)(l|1\\.|l\\-|!|\\||\\]\\[|]|\u00a3|\u222b|\u00cc|\u00cd|\u00ce|\u00cf)(e|e\\.|e\\-|3|\u20ac|\u00c8|\u00e8|\u00c9|\u00e9|\u00ca|\u00ea|\u2211)\/i","\/(w|w\\.|w\\-|\u03c9|\u03c8|\u03a8)(o|o\\.|o\\-|0|\u039f|\u03bf|\u03a6|\u00a4|\u00b0|\u00f8)(b|b\\.|b\\-|8|\\|3|\u00df|\u0392|\u03b2)(b|b\\.|b\\-|8|\\|3|\u00df|\u0392|\u03b2)(l|1\\.|l\\-|!|\\||\\]\\[|]|\u00a3|\u222b|\u00cc|\u00cd|\u00ce|\u00cf)(e|e\\.|e\\-|3|\u20ac|\u00c8|\u00e8|\u00c9|\u00e9|\u00ca|\u00ea|\u2211)\/i"]']
]
]);

parent::setUp();

}

/**
* @test
*/
public function create_discussion_without_any_bad_words()
{
$response = $this->send(
$this->request('POST', '/api/discussions', [
'authenticatedAs' => 2,
'json' => [
'data' => [
'attributes' => [
'title' => 'test - too-obscure',
'content' => 'predetermined content for automated testing - too-obscure',
],
]
],
])
);

$this->assertEquals(201, $response->getStatusCode());

/** @var Discussion $discussion */
$discussion = Discussion::firstOrFail();
$data = json_decode($response->getBody()->getContents(), true);

$this->assertEquals('test - too-obscure', $discussion->title);
$this->assertEquals('test - too-obscure', Arr::get($data, 'data.attributes.title'));

$post = $discussion->firstPost;

$this->assertNotNull($post);
$this->assertEquals('predetermined content for automated testing - too-obscure', $post->content);

$this->assertTrue($post->is_approved);
$this->assertTrue($discussion->is_approved);
}

/**
* @test
*/
public function create_discussion_with_bad_words()
{
$response = $this->send(
$this->request('POST', '/api/discussions', [
'authenticatedAs' => 2,
'json' => [
'data' => [
'attributes' => [
'title' => 'test - wibble',
'content' => 'predetermined content for automated testing - wibble',
],
]
],
])
);

$this->assertEquals(201, $response->getStatusCode());

/** @var Discussion $discussion */
$discussion = Discussion::firstOrFail();
$data = json_decode($response->getBody()->getContents(), true);

$this->assertEquals('test - wibble', $discussion->title);
$this->assertEquals('test - wibble', Arr::get($data, 'data.attributes.title'));

$post = $discussion->firstPost;

$this->assertNotNull($post);
$this->assertEquals('predetermined content for automated testing - wibble', $post->content);

$this->assertFalse($post->is_approved);
$this->assertFalse($discussion->is_approved);
}
}
16 changes: 16 additions & 0 deletions tests/integration/setup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed 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 f5129c3

Please sign in to comment.