Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] fix(core, mentions): return null if content left empty in formatter #4059

Merged
merged 11 commits into from
Oct 8, 2024
10 changes: 7 additions & 3 deletions extensions/mentions/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 extensions/mentions/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 extensions/mentions/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 extensions/mentions/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 extensions/mentions/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));
}
}
2 changes: 1 addition & 1 deletion framework/core/src/Formatter/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function unparse($xml, $context = null)
$xml = $callback($context, $xml);
}

return Unparser::unparse($xml);
return $xml !== null ? Unparser::unparse($xml) : null;
}

/**
Expand Down
Loading