From 405062e4f4eb15f4e0dd0d7a4afddb6e07d20260 Mon Sep 17 00:00:00 2001 From: Mark Kazemier Date: Wed, 7 Jul 2021 19:43:33 +0200 Subject: [PATCH] Added test coverage for add game comment page Increases test coverage (#9) And revealed issues #18, #19 and #20 --- addpost.php | 84 +++++------ tests/Pages/AddGameCommentTest.php | 235 +++++++++++++++++++++++++++++ 2 files changed, 272 insertions(+), 47 deletions(-) create mode 100644 tests/Pages/AddGameCommentTest.php diff --git a/addpost.php b/addpost.php index 92d8a35..6c27450 100644 --- a/addpost.php +++ b/addpost.php @@ -1,47 +1,37 @@ - checkSession()) || ($cUser -> checkCookie())) - { - $sQuery = "INSERT INTO berichten (berichtid, topicid, userid, bericht, datum, tijd) - VALUES ('', '" . add($_GET['topicid']) . "', '" . $cUser -> m_iUserid . "', - '" . add($_POST['reactie']) . "', NOW(), NOW());"; - if(mysql_query($sQuery)) - { - $cUser -> addPost(); - header('Location: gameview.php?id=' . $_GET['id'] . '&topicid=' . $_GET['topicid']); - } - else - { - Print 'Er is iets niet in orde met de database'; - } - } - else - { - header('Location: loginForm.php'); - } -} -else -{ - header('HTTP/1.0 404 Page not Found'); -} -?> \ No newline at end of file +checkSession()) || ($cUser->checkCookie())) { + $sQuery = "INSERT INTO berichten (berichtid, topicid, userid, bericht, datum, tijd) + VALUES ('', '" . add($_GET['topicid']) . "', '" . $cUser->m_iUserid . "', + '" . add($_POST['reactie']) . "', NOW(), NOW());"; + if (mysql_query($sQuery)) { + $cUser->addPost(); + header('Location: gameview.php?id=' . $_GET['id'] . '&topicid=' . $_GET['topicid']); + } else { + print 'Er is iets niet in orde met de database'; + } + } else { + header('Location: loginForm.php'); + } +} else { + header('HTTP/1.0 404 Page not Found'); +} \ No newline at end of file diff --git a/tests/Pages/AddGameCommentTest.php b/tests/Pages/AddGameCommentTest.php new file mode 100644 index 0000000..b4bf0a3 --- /dev/null +++ b/tests/Pages/AddGameCommentTest.php @@ -0,0 +1,235 @@ +userId = $this->login(); + + $this->consoleId = ConsoleFactory::create(self::$pdo, 'Xbox'); + + $this->gameId = GameFactory::create( + self::$pdo, + $this->consoleId, + 'Halo', + 'halo', + 'Developer', + 'Publisher', + 'http://developer.com', + 'http://publisher.com' + ); + + $this->topicId = TopicFactory::create( + self::$pdo, + $this->userId, + 'Review', + 'It is a really nice game!' + ); + TopicFactory::attach( + self::$pdo, + $this->topicId, + $this->gameId + ); + } + + /** @test */ + public function it_adds_a_game_comment_for_a_logged_in_user() + { + $page = $this->visitPage( + __DIR__ . '/../../addpost.php', + [ + 'id' => $this->gameId, + 'topicid' => $this->topicId + ], + [ + 'reactie' => 'Really good game!' + ] + ); + + $this->assertEquals('', $page); + $this->assertDatabaseHas('berichten', [ + 'topicid' => $this->topicId, + 'userid' => $this->userId, + 'bericht' => 'Really good game!' + ]); + $this->assertDatabaseHas('users', [ + 'username' => 'Mark', + 'posts' => 1 + ]); + } + + /** @test */ + public function it_shows_404_when_game_id_is_missing() + { + $page = $this->visitPage( + __DIR__ . '/../../addpost.php', + [ + 'topicid' => $this->topicId + ], + [ + 'reactie' => 'Really good game!' + ] + ); + + $this->assertEquals('', $page); + $this->assertDatabaseMissing('berichten', [ + 'topicid' => $this->topicId, + 'userid' => $this->userId, + 'bericht' => 'Really good game!' + ]); + $this->assertDatabaseHas('users', [ + 'username' => 'Mark', + 'posts' => 0 + ]); + } + + /** @test */ + public function it_shows_404_when_topic_id_is_missing() + { + $page = $this->visitPage( + __DIR__ . '/../../addpost.php', + [ + 'id' => $this->gameId + ], + [ + 'reactie' => 'Really good game!' + ] + ); + + $this->assertEquals('', $page); + $this->assertDatabaseMissing('berichten', [ + 'topicid' => $this->topicId, + 'userid' => $this->userId, + 'bericht' => 'Really good game!' + ]); + $this->assertDatabaseHas('users', [ + 'username' => 'Mark', + 'posts' => 0 + ]); + } + + /** @test */ + public function it_shows_404_when_topic_doesnt_exist() + { + $page = $this->visitPage( + __DIR__ . '/../../addpost.php', + [ + 'id' => $this->gameId, + 'topicid' => 999 + ], + [ + 'reactie' => 'Really good game!' + ] + ); + + $this->assertEquals('', $page); + $this->assertDatabaseMissing('berichten', [ + 'topicid' => 999, + 'userid' => $this->userId, + 'bericht' => 'Really good game!' + ]); + $this->assertDatabaseHas('users', [ + 'username' => 'Mark', + 'posts' => 0 + ]); + } + + /** @test */ + public function it_shows_404_when_topic_doesnt_exist_for_game() + { + $newTopicId = TopicFactory::create( + self::$pdo, + $this->userId, + 'Different topic', + 'The content of the different topic' + ); + + $page = $this->visitPage( + __DIR__ . '/../../addpost.php', + [ + 'id' => $this->gameId, + 'topicid' => $newTopicId + ], + [ + 'reactie' => 'Really good game!' + ] + ); + + $this->assertEquals('', $page); + $this->assertDatabaseMissing('berichten', [ + 'topicid' => $newTopicId, + 'userid' => $this->userId, + 'bericht' => 'Really good game!' + ]); + $this->assertDatabaseHas('users', [ + 'username' => 'Mark', + 'posts' => 0 + ]); + } + + /** @test */ + public function it_shows_404_when_comment_data_is_missing() + { + $page = $this->visitPage( + __DIR__ . '/../../addpost.php', + [ + 'id' => $this->gameId, + 'topicid' => $this->topicId + ], + [] + ); + + $this->assertEquals('', $page); + $this->assertDatabaseMissing('berichten', [ + 'topicid' => $this->topicId, + 'userid' => $this->userId, + 'bericht' => '' + ]); + $this->assertDatabaseHas('users', [ + 'username' => 'Mark', + 'posts' => 0 + ]); + } + + /** @test */ + public function it_redirects_to_login_when_user_isnt_logged_in() + { + $this->logout(); + + $page = $this->visitPage( + __DIR__ . '/../../addpost.php', + [ + 'id' => $this->gameId, + 'topicid' => $this->topicId + ], + [ + 'reactie' => 'Really good game!' + ] + ); + + $this->assertEquals('', $page); + $this->assertDatabaseMissing('berichten', [ + 'topicid' => $this->topicId, + 'userid' => $this->userId, + 'bericht' => 'Really good game!' + ]); + $this->assertDatabaseHas('users', [ + 'username' => 'Mark', + 'posts' => 0 + ]); + } +}