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
Prev Previous commit
Next Next commit
chore: move tests to mentions
  • Loading branch information
DavideIadeluca committed Oct 7, 2024
commit 4c89945f69ecf90747d76d6b72954a047acba0de
104 changes: 104 additions & 0 deletions extensions/mentions/tests/integration/api/CreateDiscussionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?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\Discussion\Discussion;
use Flarum\Extend;
use Flarum\Testing\integration\TestCase;
use Illuminate\Support\Arr;

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_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));
}
}
91 changes: 0 additions & 91 deletions framework/core/tests/integration/api/discussions/CreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,97 +69,6 @@ public function cannot_create_discussion_without_content()
], json_decode($body, true));
}

/**
* @test
*/
public function cannot_create_discussion_without_content_property()
{
$this->extend(
(new Extend\Formatter)
->unparse(function ($context, string $content) {
return $content;
}),
(new Extend\Event())
->listen(\Flarum\Post\Event\Saving::class, function ($event) {
$event->post->content;
})
);

$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()
{
$this->extend(
(new Extend\Formatter)
->unparse(function ($context, string $content) {
return $content;
}),
(new Extend\Event())
->listen(\Flarum\Post\Event\Saving::class, function ($event) {
$event->post->content;
})
);

$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));
}

/**
* @test
*/
Expand Down
Loading