mirror of
https://github.com/flarum/core.git
synced 2025-08-05 16:07:34 +02:00
[1.x] fix(core, mentions): return null if content left empty in formatter (#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
This commit is contained in:
@@ -28,12 +28,16 @@ class UnparsePostMentions
|
||||
/**
|
||||
* 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);
|
||||
|
||||
|
@@ -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);
|
||||
|
||||
|
@@ -29,12 +29,16 @@ class UnparseUserMentions
|
||||
/**
|
||||
* 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);
|
||||
|
||||
|
@@ -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
extensions/mentions/tests/integration/api/EditPostTest.php
Normal file
110
extensions/mentions/tests/integration/api/EditPostTest.php
Normal file
@@ -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));
|
||||
}
|
||||
}
|
@@ -139,7 +139,7 @@ class Formatter
|
||||
$xml = $callback($context, $xml);
|
||||
}
|
||||
|
||||
return Unparser::unparse($xml);
|
||||
return $xml !== null ? Unparser::unparse($xml) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user