Skip to content

Commit

Permalink
Added test coverage to add news comments page
Browse files Browse the repository at this point in the history
Relates to #9
  • Loading branch information
Markkaz committed Jul 7, 2021
1 parent 151df89 commit bd1257d
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 56 deletions.
95 changes: 41 additions & 54 deletions addNieuws.php
Original file line number Diff line number Diff line change
@@ -1,54 +1,41 @@
<?php
error_reporting(E_ALL);

session_start();

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

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

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

/* Verbinding met de database maken */
connectDB();

/* Permissies controleren */
if(isset($_GET['id']))
{
if(isset($_POST['reactie']))
{
if(($cUser -> checkSession()) || ($cUser -> checkCookie()))
{
$sQuery = "INSERT INTO nieuwsreacties (reactieid, nieuwsid, userid, bericht, datum, tijd)
VALUES ('', '" . add($_GET['id']) . "', '" . $cUser -> m_iUserid . "',
'" . add($_POST['reactie']) . "', NOW(), NOW());";
if(mysql_query($sQuery))
{
$cUser -> addPost();
header('Location: shownieuws.php?id=' . $_GET['id']);
}
else
{
print 'Er is iets fout gegaan met de database en/of query';
print '<br>' . $sQuery . '<br>' . mysql_error();
}
}
else
{
header('Location: loginForm.php');
}
}
else
{
header('HTTP/1.0 404 Page not Found');
}
}
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');

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

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

/* Verbinding met de database maken */
connectDB();

/* Permissies controleren */
if (isset($_GET['id'])) {
if (isset($_POST['reactie'])) {
if (($cUser->checkSession()) || ($cUser->checkCookie())) {
$sQuery = "INSERT INTO nieuwsreacties (reactieid, nieuwsid, userid, bericht, datum, tijd)
VALUES ('', '" . add($_GET['id']) . "', '" . $cUser->m_iUserid . "',
'" . add($_POST['reactie']) . "', NOW(), NOW());";
if (mysql_query($sQuery)) {
$cUser->addPost();
header('Location: shownieuws.php?id=' . $_GET['id']);
} else {
print 'Er is iets fout gegaan met de database en/of query';
print '<br>' . $sQuery . '<br>' . mysql_error();
}
} else {
header('Location: loginForm.php');
}
} else {
header('HTTP/1.0 404 Page not Found');
}
} else {
header('HTTP/1.0 404 Page not Found');
}
118 changes: 118 additions & 0 deletions tests/Pages/AddNewsCommentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace Tests\Pages;

use Tests\Factories\NewsFactory;
use Tests\TestCase;

class AddNewsCommentTest extends TestCase
{
private $userId;
private $newsId;

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

$this->userId = $this->login();

$this->newsId = NewsFactory::create(
self::$pdo,
$this->userId,
'News item',
'Body of news item'
);
}


/** @test */
public function it_adds_a_news_comment_for_a_logged_in_user()
{
$page = $this->visitPage(
__DIR__ . '/../../addNieuws.php',
['id' => $this->newsId],
['reactie' => 'A nice comment!']
);

$this->assertEquals('', $page);

$this->assertDatabaseHas('nieuwsreacties', [
'nieuwsid' => $this->newsId,
'userid' => $this->userId,
'bericht' => 'A nice comment!'
]);
}

/** @test */
public function it_shows_404_when_newsid_parameter_is_missing()
{
$page = $this->visitPage(
__DIR__ . '/../../addNieuws.php',
[],
['reactie' => 'A nice comment!']
);

$this->assertEquals('', $page);

$this->assertDatabaseMissing('nieuwsreacties', [
'nieuwsid' => $this->newsId,
'userid' => $this->userId,
'bericht' => 'A nice comment!'
]);
}

/** @test */
public function it_shows_404_when_newsitem_doesnt_exist()
{
$page = $this->visitPage(
__DIR__ . '/../../addNieuws.php',
['id' => 999],
['reactie' => 'A nice comment!']
);

$this->assertEquals('', $page);

$this->assertDatabaseMissing('nieuwsreacties', [
'nieuwsid' => 999,
'userid' => $this->userId,
'bericht' => 'A nice comment!'
]);
}

/** @test */
public function it_shows_404_when_form_wasnt_posted()
{
$page = $this->visitPage(
__DIR__ . '/../../addNieuws.php',
['id' => $this->newsId]
);

$this->assertEquals('', $page);

$this->assertDatabaseMissing('nieuwsreacties', [
'nieuwsid' => $this->newsId,
'userid' => $this->userId,
'bericht' => ''
]);
}

/** @test */
public function it_redirects_to_login_form_when_user_isnt_logged_in()
{
$this->logout();

$page = $this->visitPage(
__DIR__ . '/../../addNieuws.php',
['id' => $this->newsId],
['reactie' => 'A nice comment!']
);

$this->assertEquals('', $page);

$this->assertDatabaseMissing('nieuwsreacties', [
'nieuwsid' => $this->newsId,
'userid' => $this->userId,
'bericht' => 'A nice comment!'
]);
}
}
45 changes: 43 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ protected static function createDatabaseConnection()
{
self::$pdo = new \PDO('mysql:host=localhost;dbname=spelcodes', 'homestead', 'secret');
self::$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
self::$pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
}

protected static function createTables()
Expand Down Expand Up @@ -76,10 +77,15 @@ protected function emptyTables()
self::$pdo->query('SET FOREIGN_KEY_CHECKS=1');
}

protected function visitPage($pagePath, array $get = [])
protected function visitPage($pagePath, array $get = [], array $post = [])
{
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$_GET = $get;
$_POST = $post;

$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
if(!empty($_POST)) {
$_SERVER['REQUEST_METHOD'] = 'post';
}

session_abort();
ob_start();
Expand Down Expand Up @@ -108,4 +114,39 @@ public function login($permissions = 0)

return $userId;
}

public function logout()
{
unset($_COOKIE['USERDATA']);
}

public function assertDatabaseHas($table, array $fields)
{
$data = $this->getFieldsFromTable($table, $fields);

$this->assertContains(
$fields,
$data,
'Database contains: ' . print_r($data, true)
);
}

public function assertDatabaseMissing($table, array $fields)
{
$data = $this->getFieldsFromTable($table, $fields);

$this->assertNotContains(
$fields,
$data,
'Database contains: ' . print_r($data, true)
);
}

protected function getFieldsFromTable($table, array $fields)
{
$sql = 'SELECT ' . implode(',', array_keys($fields)) . ' FROM ' . $table . ';';
$result = self::$pdo->query($sql);

return $result->fetchAll();
}
}

0 comments on commit bd1257d

Please sign in to comment.