|
| 1 | +<?php |
| 2 | + |
| 3 | +use Carbon\Carbon; |
| 4 | +use FiveamCode\LaravelNotionApi\Entities\Collections\CommentCollection; |
| 5 | +use FiveamCode\LaravelNotionApi\Entities\Comment; |
| 6 | +use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText; |
| 7 | +use FiveamCode\LaravelNotionApi\Exceptions\NotionException; |
| 8 | +use Illuminate\Support\Facades\Http; |
| 9 | + |
| 10 | +it('should throw correct exception if comment access not allowed by api when listing comments', function () { |
| 11 | + // not_found /v1/comments |
| 12 | + Http::fake([ |
| 13 | + 'https://api.notion.com/v1/comments?block_id=cbf6b0af-6eaa-45ca-9715-9fa147ef6b17*' => Http::response( |
| 14 | + json_decode(file_get_contents('tests/stubs/endpoints/comments/response_list_comments_403.json'), true), |
| 15 | + 403, |
| 16 | + ['Headers'] |
| 17 | + ), |
| 18 | + ]); |
| 19 | + |
| 20 | + $this->expectException(NotionException::class); |
| 21 | + $this->expectExceptionMessage('Insufficient permissions for this endpoint.'); |
| 22 | + $this->expectExceptionCode(403); |
| 23 | + |
| 24 | + \Notion::comments()->ofBlock('cbf6b0af-6eaa-45ca-9715-9fa147ef6b17')->list(); |
| 25 | +}); |
| 26 | + |
| 27 | +it('should throw correct exception if block_id has not been found when listing comments', function () { |
| 28 | + // not_found /v1/comments |
| 29 | + Http::fake([ |
| 30 | + 'https://api.notion.com/v1/comments?block_id=cbf6b0af-6eaa-45ca-9715-9fa147ef6b17*' => Http::response( |
| 31 | + json_decode(file_get_contents('tests/stubs/endpoints/comments/response_list_comments_404.json'), true), |
| 32 | + 404, |
| 33 | + ['Headers'] |
| 34 | + ), |
| 35 | + ]); |
| 36 | + |
| 37 | + $this->expectException(NotionException::class); |
| 38 | + $this->expectExceptionMessage('Not Found'); |
| 39 | + $this->expectExceptionCode(404); |
| 40 | + |
| 41 | + \Notion::comments()->ofBlock('cbf6b0af-6eaa-45ca-9715-9fa147ef6b17')->list(); |
| 42 | +}); |
| 43 | + |
| 44 | + |
| 45 | +it('should fetch list of comments with an accurate representation of attributes', function () { |
| 46 | + |
| 47 | + // successfull /v1/comments |
| 48 | + Http::fake([ |
| 49 | + 'https://api.notion.com/v1/comments?block_id=abf6b0af-6eaa-45ca-9715-9fa147ef6b17*' => Http::response( |
| 50 | + json_decode(file_get_contents('tests/stubs/endpoints/comments/response_list_comments_200.json'), true), |
| 51 | + 200, |
| 52 | + ['Headers'] |
| 53 | + ), |
| 54 | + ]); |
| 55 | + |
| 56 | + $commentCollection = \Notion::comments()->ofBlock('abf6b0af-6eaa-45ca-9715-9fa147ef6b17'); |
| 57 | + |
| 58 | + $collection = $commentCollection->asCollection(); |
| 59 | + $json = $commentCollection->asJson(); |
| 60 | + |
| 61 | + expect($commentCollection)->toBeInstanceOf(CommentCollection::class); |
| 62 | + expect($collection)->toBeInstanceOf(\Illuminate\Support\Collection::class); |
| 63 | + expect($json)->toBeString(); |
| 64 | + |
| 65 | + expect($collection->count())->toBe(1); |
| 66 | + expect($collection->first())->toBeInstanceOf(Comment::class); |
| 67 | + expect($collection->first()->getObjectType())->toBe('comment'); |
| 68 | + expect($collection->first()->getId())->toBe('94cc56ab-9f02-409d-9f99-1037e9fe502f'); |
| 69 | + expect($collection->first()->getCreatedTime())->toEqual(Carbon::parse('2022-07-15T16:52:00.000Z')->toDateTime()); |
| 70 | + expect($collection->first()->getLastEditedTime())->toEqual(Carbon::parse('2022-07-15T19:16:00.000Z')->toDateTime()); |
| 71 | + expect($collection->first()->getCreatedBy()->getId())->toBe('9b15170a-9941-4297-8ee6-83fa7649a87a'); |
| 72 | + expect($collection->first()->getText())->toBe('Single comment'); |
| 73 | + expect($collection->first()->getRichText()->getPlainText())->toBe('Single comment'); |
| 74 | + expect($collection->first()->getRichText())->toBeInstanceOf(RichText::class); |
| 75 | + expect($collection->first()->getParentId())->toBe('5c6a2821-6bb1-4a7e-b6e1-c50111515c3d'); |
| 76 | + expect($collection->first()->getParentType())->toBe('page_id'); |
| 77 | + expect($collection->first()->getDiscussionId())->toBe('f1407351-36f5-4c49-a13c-49f8ba11776d'); |
| 78 | + |
| 79 | + expect($json)->toBeJson(); |
| 80 | +}); |
| 81 | + |
| 82 | + |
| 83 | +it('should throw correct exception if comment access not allowed by api when creating a comment', function () { |
| 84 | + // not_found /v1/comments |
| 85 | + Http::fake([ |
| 86 | + 'https://api.notion.com/v1/comments*' => Http::response( |
| 87 | + json_decode(file_get_contents('tests/stubs/endpoints/comments/response_create_comment_403.json'), true), |
| 88 | + 403, |
| 89 | + ['Headers'] |
| 90 | + ), |
| 91 | + ]); |
| 92 | + |
| 93 | + $this->expectException(NotionException::class); |
| 94 | + $this->expectExceptionMessage('Insufficient permissions for this endpoint.'); |
| 95 | + $this->expectExceptionCode(403); |
| 96 | + |
| 97 | + \Notion::comments()->onPage('5c6a2821-6bb1-4a7e-b6e1-c50111515c3d')->create(Comment::create('Hello world')); |
| 98 | +}); |
| 99 | + |
| 100 | +it('should throw correct exception if discussion is not found with discussion_id when creating a comment', function () { |
| 101 | + // not_found (post) /v1/comments |
| 102 | + Http::fake([ |
| 103 | + 'https://api.notion.com/v1/comments*' => Http::response( |
| 104 | + json_decode(file_get_contents('tests/stubs/endpoints/comments/response_create_comment_404.json'), true), |
| 105 | + 404, |
| 106 | + ['Headers'] |
| 107 | + ), |
| 108 | + ]); |
| 109 | + |
| 110 | + $this->expectException(NotionException::class); |
| 111 | + $this->expectExceptionMessage('Could not find discussion with ID: 141216d8-bbc5-4c24-9d37-3c45d3bc15cc.'); |
| 112 | + $this->expectExceptionCode(404); |
| 113 | + |
| 114 | + \Notion::comments()->onDiscussion('141216d8-bbc5-4c24-9d37-3c45d3bc15cc')->create(Comment::create('Hello world')); |
| 115 | +}); |
| 116 | + |
| 117 | +it('successfully creates a comment within a page', function(){ |
| 118 | + |
| 119 | + // successfull (post) /v1/comments |
| 120 | + Http::fake([ |
| 121 | + 'https://api.notion.com/v1/comments*' => Http::response( |
| 122 | + json_decode(file_get_contents('tests/stubs/endpoints/comments/response_create_comment_200.json'), true), |
| 123 | + 200, |
| 124 | + ['Headers'] |
| 125 | + ), |
| 126 | + ]); |
| 127 | + |
| 128 | + $comment = \Notion::comments()->onPage('5c6a2821-6bb1-4a7e-b6e1-c50111515c3d')->create(Comment::create('Hello world')); |
| 129 | + |
| 130 | + expect($comment)->toBeInstanceOf(Comment::class); |
| 131 | + expect($comment->getObjectType())->toBe('comment'); |
| 132 | + expect($comment->getId())->toBe('b52b8ed6-e029-4707-a671-832549c09de3'); |
| 133 | + expect($comment->getCreatedTime())->toEqual(Carbon::parse('2022-07-15T20:53:00.000Z')->toDateTime()); |
| 134 | + expect($comment->getLastEditedTime())->toEqual(Carbon::parse('2022-07-15T20:53:00.000Z')->toDateTime()); |
| 135 | + expect($comment->getCreatedBy()->getId())->toBe('067dee40-6ebd-496f-b446-093c715fb5ec'); |
| 136 | + expect($comment->getText())->toBe('Hello world'); |
| 137 | + expect($comment->getRichText()->getPlainText())->toBe('Hello world'); |
| 138 | + expect($comment->getRichText())->toBeInstanceOf(RichText::class); |
| 139 | + expect($comment->getParentId())->toBe('5c6a2821-6bb1-4a7e-b6e1-c50111515c3d'); |
| 140 | + expect($comment->getParentType())->toBe('page_id'); |
| 141 | + expect($comment->getDiscussionId())->toBe('f1407351-36f5-4c49-a13c-49f8ba11776d'); |
| 142 | +}); |
| 143 | + |
| 144 | + |
0 commit comments