Skip to content

Commit

Permalink
[1.x] fix(core, mentions): return null if content left empty in forma…
Browse files Browse the repository at this point in the history
…tter (#4059)

* test(core): implement test for creating discussion without content

* fix(core): handle `null` case in XML parser

* fix(mentions): change/remove typings in unparser

* fix(mentions): return early if xml null

* chore: fix PHPStan

* chore: move tests to mentions

* chore: remove unused import

* chore: remove unused imports

* test(mentions): implement test for post editing with content empty

* test(mentions): change post edit tests

* test(mentions): add test for creating discussion with empty string
  • Loading branch information
DavideIadeluca authored Oct 8, 2024
1 parent 8f13f73 commit 9f1d786
Show file tree
Hide file tree
Showing 5 changed files with 268 additions and 9 deletions.
10 changes: 7 additions & 3 deletions src/Formatter/UnparsePostMentions.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ public function __construct(TranslatorInterface $translator)
/**
* Configure rendering for user mentions.
*
* @param string $xml
* @param string|null $xml
* @param mixed $context
* @return string $xml to be unparsed
* @return mixed $xml to be unparsed
*/
public function __invoke($context, string $xml)
public function __invoke($context, $xml)
{
if ($xml === null) {
return $xml;
}

$xml = $this->updatePostMentionTags($context, $xml);
$xml = $this->unparsePostMentionTags($xml);

Expand Down
10 changes: 7 additions & 3 deletions src/Formatter/UnparseTagMentions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ class UnparseTagMentions
/**
* Configure rendering for user mentions.
*
* @param string $xml
* @param string|null $xml
* @param mixed $context
* @return string $xml to be unparsed
* @return mixed $xml to be unparsed
*/
public function __invoke($context, string $xml)
public function __invoke($context, $xml)
{
if ($xml === null) {
return $xml;
}

$xml = $this->updateTagMentionTags($context, $xml);
$xml = $this->unparseTagMentionTags($xml);

Expand Down
10 changes: 7 additions & 3 deletions src/Formatter/UnparseUserMentions.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ public function __construct(TranslatorInterface $translator)
/**
* Configure rendering for user mentions.
*
* @param string $xml
* @param string|null $xml
* @param mixed $context
* @return string $xml to be unparsed
* @return mixed $xml to be unparsed
*/
public function __invoke($context, string $xml)
public function __invoke($context, $xml)
{
if ($xml === null) {
return $xml;
}

$xml = $this->updateUserMentionTags($context, $xml);
$xml = $this->unparseUserMentionTags($xml);

Expand Down
137 changes: 137 additions & 0 deletions tests/integration/api/CreateDiscussionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?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.
*/

namespace Flarum\Mentions\Tests\integration\api;

use Flarum\Extend;
use Flarum\Testing\integration\TestCase;

class CreateDiscussionTest extends TestCase
{
/**
* @inheritDoc
*/
protected function setUp(): void
{
parent::setUp();

$this->extension('flarum-mentions');

$this->extend(
(new Extend\Event())
->listen(\Flarum\Post\Event\Saving::class, function ($event) {
$event->post->content;
})
);
}

/**
* @test
*/
public function cannot_create_discussion_with_empty_string()
{
$response = $this->send(
$this->request('POST', '/api/discussions', [
'authenticatedAs' => 1,
'json' => [
'data' => [
'attributes' => [
'title' => 'Test post',
'content' => '',
],
],
],
])
);

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

$body = (string) $response->getBody();
$this->assertJson($body);
$this->assertEquals([
'errors' => [
[
'status' => '422',
'code' => 'validation_error',
'detail' => 'The content field is required.',
'source' => ['pointer' => '/data/attributes/content'],
],
],
], json_decode($body, true));
}

/**
* @test
*/
public function cannot_create_discussion_without_content_property()
{
$response = $this->send(
$this->request('POST', '/api/discussions', [
'authenticatedAs' => 1,
'json' => [
'data' => [
'attributes' => [
'title' => 'Test post',
],
],
],
])
);

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

$body = (string) $response->getBody();
$this->assertJson($body);
$this->assertEquals([
'errors' => [
[
'status' => '422',
'code' => 'validation_error',
'detail' => 'The content field is required.',
'source' => ['pointer' => '/data/attributes/content'],
],
],
], json_decode($body, true));
}

/**
* @test
*/
public function cannot_create_discussion_with_content_set_to_null()
{
$response = $this->send(
$this->request('POST', '/api/discussions', [
'authenticatedAs' => 1,
'json' => [
'data' => [
'attributes' => [
'title' => 'Test post',
'content' => null,
],
],
],
])
);

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

$body = (string) $response->getBody();
$this->assertJson($body);
$this->assertEquals([
'errors' => [
[
'status' => '422',
'code' => 'validation_error',
'detail' => 'The content field is required.',
'source' => ['pointer' => '/data/attributes/content'],
],
],
], json_decode($body, true));
}
}
110 changes: 110 additions & 0 deletions tests/integration/api/EditPostTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?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.
*/

namespace Flarum\Mentions\Tests\integration\api;

use Flarum\Extend;
use Flarum\Testing\integration\TestCase;

class EditPostTest extends TestCase
{
/**
* @inheritDoc
*/
protected function setUp(): void
{
parent::setUp();

$this->extension('flarum-mentions');

$this->prepareDatabase([
'discussions' => [
['id' => 1, 'title' => 'Discussion with post', 'user_id' => 1, 'first_post_id' => 1, 'comment_count' => 1],
],
'posts' => [
['id' => 1, 'discussion_id' => 1, 'user_id' => 1, 'type' => 'comment', 'content' => '<t><p>Text</p></t>'],
]
]);

$this->extend(
(new Extend\Event())
->listen(\Flarum\Post\Event\Saving::class, function ($event) {
$event->post->content;
})
);
}

/**
* @test
*/
public function cannot_update_post_with_empty_string()
{
$response = $this->send(
$this->request('PATCH', '/api/posts/1', [
'authenticatedAs' => 1,
'json' => [
'data' => [
'attributes' => [
'content' => '',
],
],
],
])
);

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

$body = (string) $response->getBody();
$this->assertJson($body);
$this->assertEquals([
'errors' => [
[
'status' => '422',
'code' => 'validation_error',
'detail' => 'The content field is required.',
'source' => ['pointer' => '/data/attributes/content'],
],
],
], json_decode($body, true));
}

/**
* @test
*/
public function cannot_update_post_with_invalid_content_type()
{
$response = $this->send(
$this->request('PATCH', '/api/posts/1', [
'authenticatedAs' => 1,
'json' => [
'data' => [
'attributes' => [
'content' => [],
],
],
],
])
);

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

$body = (string) $response->getBody();
$this->assertJson($body);
$this->assertEquals([
'errors' => [
[
'status' => '422',
'code' => 'validation_error',
'detail' => 'The content field is required.',
'source' => ['pointer' => '/data/attributes/content'],
],
],
], json_decode($body, true));
}
}

0 comments on commit 9f1d786

Please sign in to comment.