|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Unit; |
| 6 | + |
| 7 | +use Advoor\NovaEditorJs\NovaEditorJsCast; |
| 8 | +use Advoor\NovaEditorJs\NovaEditorJsData; |
| 9 | +use Tests\TestCase; |
| 10 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 11 | +use Illuminate\Support\Facades\DB; |
| 12 | +use Tests\Fixtures\Models\Dummy; |
| 13 | + |
| 14 | +class NovaEditorJsCastTest extends TestCase |
| 15 | +{ |
| 16 | + use RefreshDatabase; |
| 17 | + |
| 18 | + private const VALID_BLOCK = [ |
| 19 | + 'time' => 1658064476, |
| 20 | + 'blocks' => [ |
| 21 | + [ |
| 22 | + 'type' => 'paragraph', |
| 23 | + 'data' => [ |
| 24 | + 'text' => 'Paragraph' |
| 25 | + ] |
| 26 | + ] |
| 27 | + ], |
| 28 | + 'version' => '2.3.0', |
| 29 | + ]; |
| 30 | + |
| 31 | + /** |
| 32 | + * Test a very basic save-and-decode. |
| 33 | + */ |
| 34 | + public function testSavingAndDecoding(): void |
| 35 | + { |
| 36 | + Dummy::create(['data' => self::VALID_BLOCK]); |
| 37 | + |
| 38 | + $instance = Dummy::first(); |
| 39 | + |
| 40 | + $this->assertInstanceOf(NovaEditorJsData::class, $instance->data); |
| 41 | + $this->assertEquals(self::VALID_BLOCK, $instance->data->getAttributes()); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Test saving and decoding a value that's already JSON-encoded. |
| 46 | + */ |
| 47 | + public function testSavingPreCompiledJson(): void |
| 48 | + { |
| 49 | + Dummy::create(['data' => json_encode(self::VALID_BLOCK)]); |
| 50 | + |
| 51 | + $instance = Dummy::first(); |
| 52 | + |
| 53 | + $this->assertInstanceOf(NovaEditorJsData::class, $instance->data); |
| 54 | + $this->assertEquals(self::VALID_BLOCK, $instance->data->getAttributes()); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Test decoding a value that's double-JSON-encoded, basically bug mitigation. |
| 59 | + */ |
| 60 | + public function testReadingDoubleEncodedJson(): void |
| 61 | + { |
| 62 | + DB::statement('INSERT INTO `dummies` (`data`) VALUES (?)', [json_encode(json_encode(self::VALID_BLOCK))]); |
| 63 | + |
| 64 | + $instance = Dummy::first(); |
| 65 | + |
| 66 | + $this->assertInstanceOf(NovaEditorJsData::class, $instance->data); |
| 67 | + $this->assertEquals(self::VALID_BLOCK, $instance->data->getAttributes()); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Test reading null values in JSON. |
| 72 | + */ |
| 73 | + public function testReadingNullValues(): void |
| 74 | + { |
| 75 | + DB::statement('INSERT INTO `dummies` (`data`) VALUES (?), (null)', [json_encode(null)]); |
| 76 | + |
| 77 | + [$jsonInstance, $nullInstance] = Dummy::get(); |
| 78 | + |
| 79 | + $this->assertNull($jsonInstance->data); |
| 80 | + $this->assertNull($nullInstance->data); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Test writing null values, a json-encoded null value |
| 85 | + * should be stored as-is. |
| 86 | + */ |
| 87 | + public function testReadingNullValue(): void |
| 88 | + { |
| 89 | + Dummy::create(['data' => null]); |
| 90 | + Dummy::create(['data' => json_encode(null)]); |
| 91 | + |
| 92 | + $rows = DB::select('SELECT `id`, `data` FROM `dummies`'); |
| 93 | + |
| 94 | + $this->assertNull($rows[0]->data); |
| 95 | + $this->assertSame('null', $rows[1]->data); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Finally, check if reading broken JSON is handled properly. |
| 100 | + */ |
| 101 | + public function testReadingInvalidJson(): void |
| 102 | + { |
| 103 | + DB::statement('INSERT INTO `dummies` (`data`) VALUES (?)', ['{"}']); |
| 104 | + |
| 105 | + $instance = Dummy::first(); |
| 106 | + |
| 107 | + $this->assertInstanceOf(NovaEditorJsData::class, $instance->data); |
| 108 | + $this->assertNotNull($instance->data->version, 'Expected version key on data'); |
| 109 | + $this->assertEquals(NovaEditorJsCast::BROKEN_VERSION, $instance->data->version, 'Expected version to match "broken" version'); |
| 110 | + } |
| 111 | +} |
0 commit comments