-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added test coverage to add news comments page
Relates to #9
- Loading branch information
Showing
3 changed files
with
202 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!' | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters