Skip to content

Commit

Permalink
Added test coverage for add game comment page
Browse files Browse the repository at this point in the history
Increases test coverage (#9)
And revealed issues #18, #19 and #20
  • Loading branch information
Markkaz committed Jul 7, 2021
1 parent ba09237 commit 405062e
Show file tree
Hide file tree
Showing 2 changed files with 272 additions and 47 deletions.
84 changes: 37 additions & 47 deletions addpost.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,37 @@
<?php
error_reporting(E_ALL);

session_start();

/* Classes importeren */
include('Classes/User.php');
include('Classes/Template.php');

/* Includes importeren */
include('Includes/connect.php');
include('Includes/slashes.php');

/* Classes initialiseren */
$cUser = new User();

/* Verbinden met mysql */
connectDB();

/* Permissies controleren */
if((isset($_GET['topicid'])) && (isset($_POST['reactie'])) && (isset($_GET['id'])))
{
if(($cUser -> 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');
}
?>
<?php
error_reporting(E_ALL & ~E_DEPRECATED);

session_start();

/* Classes importeren */
include_once('Classes/User.php');
include_once('Classes/Template.php');

/* Includes importeren */
include_once('Includes/connect.php');
include_once('Includes/slashes.php');

/* Classes initialiseren */
$cUser = new User();

/* Verbinden met mysql */
connectDB();

/* Permissies controleren */
if ((isset($_GET['topicid'])) && (isset($_POST['reactie'])) && (isset($_GET['id']))) {
if (($cUser->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');
}
235 changes: 235 additions & 0 deletions tests/Pages/AddGameCommentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
<?php

namespace Tests\Pages;

use Tests\Factories\ConsoleFactory;
use Tests\Factories\GameFactory;
use Tests\Factories\TopicFactory;
use Tests\TestCase;

class AddGameCommentTest extends TestCase
{
private $userId;
private $consoleId;
private $gameId;
private $topicId;

protected function setUp()
{
parent::setUp();

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

0 comments on commit 405062e

Please sign in to comment.