-
-
Notifications
You must be signed in to change notification settings - Fork 835
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[1.x] fix(core, mentions): return null if content left empty in forma…
…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
1 parent
8169550
commit 9c91c89
Showing
6 changed files
with
269 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
extensions/mentions/tests/integration/api/CreateDiscussionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
110
extensions/mentions/tests/integration/api/EditPostTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters