]> BookStack Code Mirror - bookstack/blobdiff - tests/Entity/CommentTest.php
Tiny header
[bookstack] / tests / Entity / CommentTest.php
index 86eb31213204bc580201b6a4c6b2941c98782f63..1e8ecbcac2cad0a1bda62c38303e2575df3ae775 100644 (file)
-<?php namespace Tests;
+<?php
 
-use BookStack\Page;
-use BookStack\Comment;
+namespace Tests\Entity;
 
-class CommentTest extends BrowserKitTest
-{
+use BookStack\Actions\Comment;
+use BookStack\Entities\Models\Page;
+use Tests\TestCase;
 
+class CommentTest extends TestCase
+{
     public function test_add_comment()
     {
         $this->asAdmin();
-        $page = $this->getPage();
+        $page = Page::first();
 
-        $this->addComment($page);
-    }
+        $comment = Comment::factory()->make(['parent_id' => 2]);
+        $resp = $this->postJson("/comment/$page->id", $comment->getAttributes());
 
-    public function test_comment_reply()
-    {
-        $this->asAdmin();
-        $page = $this->getPage();
+        $resp->assertStatus(200);
+        $resp->assertSee($comment->text);
 
-        // add a normal comment
-        $createdComment = $this->addComment($page);
+        $pageResp = $this->get($page->getUrl());
+        $pageResp->assertSee($comment->text);
 
-        // reply to the added comment
-        $this->addComment($page, $createdComment['id']);
+        $this->assertDatabaseHas('comments', [
+            'local_id'    => 1,
+            'entity_id'   => $page->id,
+            'entity_type' => Page::newModelInstance()->getMorphClass(),
+            'text'        => $comment->text,
+            'parent_id'   => 2,
+        ]);
     }
 
     public function test_comment_edit()
     {
         $this->asAdmin();
-        $page = $this->getPage();
-
-        $createdComment = $this->addComment($page);
-        $comment = [
-            'id' => $createdComment['id'],
-            'page_id' => $createdComment['page_id']
-        ];
-        $this->updateComment($comment);
-    }
+        $page = Page::first();
 
-    public function test_comment_delete()
-    {
-        $this->asAdmin();
-        $page = $this->getPage();
+        $comment = Comment::factory()->make();
+        $this->postJson("/comment/$page->id", $comment->getAttributes());
 
-        $createdComment = $this->addComment($page);
+        $comment = $page->comments()->first();
+        $newText = 'updated text content';
+        $resp = $this->putJson("/comment/$comment->id", [
+            'text' => $newText,
+        ]);
 
-        $this->deleteComment($createdComment['id']);
-    }
+        $resp->assertStatus(200);
+        $resp->assertSee($newText);
+        $resp->assertDontSee($comment->text);
 
-    private function getPage() {
-        $page = Page::first();
-        return $page;
+        $this->assertDatabaseHas('comments', [
+            'text'      => $newText,
+            'entity_id' => $page->id,
+        ]);
     }
 
+    public function test_comment_delete()
+    {
+        $this->asAdmin();
+        $page = Page::first();
 
-    private function addComment($page, $parentCommentId = null) {
-        $comment = factory(Comment::class)->make();
-        $url = "/ajax/page/$page->id/comment/";
-        $request = [
-            'text' => $comment->text,
-            'html' => $comment->html
-        ];
-        if (!empty($parentCommentId)) {
-            $request['parent_id'] = $parentCommentId;
-        }
-        $this->call('POST', $url, $request);
-
-        $createdComment = $this->checkResponse();
-        return $createdComment;
-    }
+        $comment = Comment::factory()->make();
+        $this->postJson("/comment/$page->id", $comment->getAttributes());
 
-    private function updateComment($comment) {
-        $tmpComment = factory(Comment::class)->make();
-        $url = '/ajax/page/' . $comment['page_id'] . '/comment/ ' . $comment['id'];
-         $request = [
-            'text' => $tmpComment->text,
-            'html' => $tmpComment->html
-        ];
+        $comment = $page->comments()->first();
 
-        $this->call('PUT', $url, $request);
+        $resp = $this->delete("/comment/$comment->id");
+        $resp->assertStatus(200);
 
-        $updatedComment = $this->checkResponse();
-        return $updatedComment;
+        $this->assertDatabaseMissing('comments', [
+            'id' => $comment->id,
+        ]);
     }
 
-    private function deleteComment($commentId) {
-        //  Route::delete('/ajax/comment/{id}', 'CommentController@destroy');
-        $url = '/ajax/comment/' . $commentId;
-        $this->call('DELETE', $url);
-
-        $deletedComment = $this->checkResponse();
-        return $deletedComment;
+    public function test_comments_converts_markdown_input_to_html()
+    {
+        $page = Page::first();
+        $this->asAdmin()->postJson("/comment/$page->id", [
+            'text' => '# My Title',
+        ]);
+
+        $this->assertDatabaseHas('comments', [
+            'entity_id'   => $page->id,
+            'entity_type' => $page->getMorphClass(),
+            'text'        => '# My Title',
+            'html'        => "<h1>My Title</h1>\n",
+        ]);
+
+        $pageView = $this->get($page->getUrl());
+        $pageView->assertSee('<h1>My Title</h1>', false);
     }
 
-    private function checkResponse() {
-        $expectedResp = [
-            'status' => 'success'
-        ];
+    public function test_html_cannot_be_injected_via_comment_content()
+    {
+        $this->asAdmin();
+        $page = Page::first();
+
+        $script = '<script>const a = "script";</script>\n\n# sometextinthecomment';
+        $this->postJson("/comment/$page->id", [
+            'text' => $script,
+        ]);
 
-        $this->assertResponseOk();
-        $this->seeJsonContains($expectedResp);
+        $pageView = $this->get($page->getUrl());
+        $pageView->assertDontSee($script, false);
+        $pageView->assertSee('sometextinthecomment');
 
-        $resp = $this->decodeResponseJson();
-        $createdComment = $resp['comment'];
-        $this->assertArrayHasKey('id', $createdComment);
+        $comment = $page->comments()->first();
+        $this->putJson("/comment/$comment->id", [
+            'text' => $script . 'updated',
+        ]);
 
-        return $createdComment;
+        $pageView = $this->get($page->getUrl());
+        $pageView->assertDontSee($script, false);
+        $pageView->assertSee('sometextinthecommentupdated');
     }
 }