-
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 show news page
Added test coverage: #9
- Loading branch information
Showing
4 changed files
with
273 additions
and
85 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,79 +1,70 @@ | ||
<?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'); | ||
include('Includes/smiley.php'); | ||
include('Includes/bbcode.php'); | ||
|
||
/* Classes initialiseren */ | ||
$cUser = new user(); | ||
$cTPL = new Template('Templates/main.tpl'); | ||
|
||
/* Verbinding met de database maken */ | ||
connectDB(); | ||
|
||
/* Controleren of er wel een id is meegegeven */ | ||
if(isset($_GET['id'])) | ||
{ | ||
include('Includes/login.php'); | ||
|
||
$cTPL -> setFile('CONTENT', 'Templates/shownieuws.tpl'); | ||
$cTPL -> parse(); | ||
|
||
$sQuery = "SELECT n.titel, n.bericht, u.username, n.datum, n.tijd FROM nieuws n, users u | ||
WHERE u.userid = n.userid AND n.nieuwsid='" . add($_GET['id']) . "';"; | ||
if($cResult = mysql_query($sQuery)) | ||
{ | ||
$aData = mysql_fetch_assoc($cResult); | ||
$cTPL -> setPlace('TITEL', add($aData['titel'])); | ||
$cTPL -> setPlace('USERNAME', add($aData['username'])); | ||
$cTPL -> setPlace('BERICHT', smiley(strip($aData['bericht']))); | ||
$cTPL -> setPlace('DATUM', add($aData['datum'] . ' ' . $aData['tijd'])); | ||
$cTPL -> parse(); | ||
} | ||
|
||
$cTPL -> setPlace('ID', $_GET['id']); | ||
|
||
$sQuery = "SELECT n.reactieid, n.bericht, u.userid, u.username, n.datum, n.tijd FROM nieuwsreacties n, users u | ||
WHERE n.userid=u.userid AND n.nieuwsid='" . add($_GET['id']) . "' | ||
ORDER BY n.datum, n.tijd;"; | ||
if($cResult = mysql_query($sQuery)) | ||
{ | ||
while($aData = mysql_fetch_assoc($cResult)) | ||
{ | ||
$cTPL -> setBlock('REACTIES', 'reacties'); | ||
$cTPL -> parse(); | ||
|
||
$cTPL -> setPlace('AUTEUR', add($aData['username'])); | ||
$cTPL -> setPlace('MOMENT', add($aData['datum'] . ' ' . $aData['tijd'])); | ||
$cTPL -> setPlace('REACTIE', bbcode(smiley(strip_tags(strip($aData['bericht']))))); | ||
|
||
if(($cUser -> m_iPermis & 2) || ($aData['userid'] == $cUser -> m_iUserid)) | ||
{ | ||
$cTPL -> setBlock('REACTIEEDIT', 'edit'); | ||
$cTPL -> parse(); | ||
$cTPL -> setPlace('REACTIEID', $aData['reactieid']); | ||
} | ||
else | ||
{ | ||
$cTPL -> setPlace('REACTIEEDIT', ''); | ||
} | ||
$cTPL -> parse(); | ||
} | ||
} | ||
|
||
$cTPL -> show(); | ||
} | ||
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'); | ||
include_once('Includes/smiley.php'); | ||
include_once('Includes/bbcode.php'); | ||
|
||
/* Classes initialiseren */ | ||
$cUser = new user(); | ||
$cTPL = new Template('Templates/main.tpl'); | ||
|
||
/* Verbinding met de database maken */ | ||
connectDB(); | ||
|
||
/* Controleren of er wel een id is meegegeven */ | ||
if (isset($_GET['id'])) { | ||
include('Includes/login.php'); | ||
|
||
$cTPL->setFile('CONTENT', 'Templates/shownieuws.tpl'); | ||
$cTPL->parse(); | ||
|
||
$sQuery = "SELECT n.titel, n.bericht, u.username, n.datum, n.tijd FROM nieuws n, users u | ||
WHERE u.userid = n.userid AND n.nieuwsid='" . add($_GET['id']) . "';"; | ||
if ($cResult = mysql_query($sQuery)) { | ||
$aData = mysql_fetch_assoc($cResult); | ||
$cTPL->setPlace('TITEL', add($aData['titel'])); | ||
$cTPL->setPlace('USERNAME', add($aData['username'])); | ||
$cTPL->setPlace('BERICHT', smiley(strip($aData['bericht']))); | ||
$cTPL->setPlace('DATUM', add($aData['datum'] . ' ' . $aData['tijd'])); | ||
$cTPL->parse(); | ||
} | ||
|
||
$cTPL->setPlace('ID', $_GET['id']); | ||
|
||
$sQuery = "SELECT n.reactieid, n.bericht, u.userid, u.username, n.datum, n.tijd FROM nieuwsreacties n, users u | ||
WHERE n.userid=u.userid AND n.nieuwsid='" . add($_GET['id']) . "' | ||
ORDER BY n.datum, n.tijd;"; | ||
if ($cResult = mysql_query($sQuery)) { | ||
while ($aData = mysql_fetch_assoc($cResult)) { | ||
$cTPL->setBlock('REACTIES', 'reacties'); | ||
$cTPL->parse(); | ||
|
||
$cTPL->setPlace('AUTEUR', add($aData['username'])); | ||
$cTPL->setPlace('MOMENT', add($aData['datum'] . ' ' . $aData['tijd'])); | ||
$cTPL->setPlace('REACTIE', bbcode(smiley(strip_tags(strip($aData['bericht']))))); | ||
|
||
if (($cUser->m_iPermis & 2) || ($aData['userid'] == $cUser->m_iUserid)) { | ||
$cTPL->setBlock('REACTIEEDIT', 'edit'); | ||
$cTPL->parse(); | ||
$cTPL->setPlace('REACTIEID', $aData['reactieid']); | ||
} else { | ||
$cTPL->setPlace('REACTIEEDIT', ''); | ||
} | ||
|
||
$cTPL->parse(); | ||
} | ||
} | ||
|
||
$cTPL->show(); | ||
} 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,22 @@ | ||
<?php | ||
|
||
namespace Tests\Factories; | ||
|
||
class NewsCommentFactory | ||
{ | ||
public static function create(\PDO $pdo, $nieuwsId, $userId, $body) | ||
{ | ||
$sql = 'INSERT INTO nieuwsreacties | ||
(nieuwsid, userid, bericht, datum, tijd) | ||
VALUES | ||
(?, ?, ?, NOW(), NOW());'; | ||
$query = $pdo->prepare($sql); | ||
$query->execute([ | ||
$nieuwsId, | ||
$userId, | ||
$body | ||
]); | ||
|
||
return $pdo->lastInsertId(); | ||
} | ||
} |
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,179 @@ | ||
<?php | ||
|
||
namespace Tests\Pages; | ||
|
||
use Tests\Factories\NewsCommentFactory; | ||
use Tests\Factories\NewsFactory; | ||
use Tests\Factories\UserFactory; | ||
use Tests\TestCase; | ||
use Webdevils\Spelcodes\Permissions; | ||
|
||
class ShowNewsTest extends TestCase | ||
{ | ||
private $userId; | ||
private $newsId; | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->userId = UserFactory::create( | ||
self::$pdo, | ||
'Mark', | ||
'secret', | ||
'[email protected]', | ||
'127.0.0.1' | ||
); | ||
|
||
$this->newsId = NewsFactory::create( | ||
self::$pdo, | ||
$this->userId, | ||
'News item', | ||
'The content of the news item' | ||
); | ||
} | ||
|
||
|
||
/** @test */ | ||
public function it_shows_the_requested_newsitem() | ||
{ | ||
$page = $this->visitPage( | ||
__DIR__ . '/../../shownieuws.php', | ||
['id' => $this->newsId] | ||
); | ||
|
||
$this->assertContains('News item', $page); | ||
$this->assertContains('The content of the news item', $page); | ||
} | ||
|
||
/** @test */ | ||
public function it_shows_comments_under_a_newsitem() | ||
{ | ||
NewsCommentFactory::create( | ||
self::$pdo, | ||
$this->newsId, | ||
$this->userId, | ||
'Nice article!' | ||
); | ||
|
||
$newsId = NewsFactory::create( | ||
self::$pdo, | ||
$this->newsId, | ||
'Different news item', | ||
'Different news item' | ||
); | ||
NewsCommentFactory::create( | ||
self::$pdo, | ||
$newsId, | ||
$this->userId, | ||
'Not visible' | ||
); | ||
|
||
$page = $this->visitPage( | ||
__DIR__ . '/../../shownieuws.php', | ||
['id' => $this->newsId] | ||
); | ||
|
||
$this->assertContains('Nice article!', $page); | ||
$this->assertNotContains('Not visible', $page); | ||
} | ||
|
||
/** @test */ | ||
public function it_shows_404_when_no_newsid_is_provided() | ||
{ | ||
$page = $this->visitPage( | ||
__DIR__ . '/../../shownieuws.php' | ||
); | ||
|
||
$this->assertEquals('', $page); | ||
} | ||
|
||
/** @test */ | ||
public function it_shows_404_when_newsid_doesnt_exist() | ||
{ | ||
$page = $this->visitPage( | ||
__DIR__ . '/../../shownieuws.php', | ||
['id' => 999] | ||
); | ||
|
||
$this->assertEquals('', $page); | ||
} | ||
|
||
/** @test */ | ||
public function it_shows_edit_comment_button_for_users_own_comments() | ||
{ | ||
$userId = $this->login(); | ||
|
||
$commentId = NewsCommentFactory::create( | ||
self::$pdo, | ||
$this->newsId, | ||
$userId, | ||
'Nice article!' | ||
); | ||
|
||
$page = $this->visitPage( | ||
__DIR__ . '/../../shownieuws.php', | ||
['id' => $this->newsId] | ||
); | ||
|
||
$this->assertContains('nieuwsEdit.php?id=' . $commentId, $page); | ||
} | ||
|
||
/** @test */ | ||
public function it_shows_edit_comment_button_for_user_with_permissions() | ||
{ | ||
$this->login(Permissions::MANAGE_COMMENTS); | ||
|
||
$commentId = NewsCommentFactory::create( | ||
self::$pdo, | ||
$this->newsId, | ||
$this->userId, | ||
'Nice article!' | ||
); | ||
|
||
$page = $this->visitPage( | ||
__DIR__ . '/../../shownieuws.php', | ||
['id' => $this->newsId] | ||
); | ||
|
||
$this->assertContains('nieuwsEdit.php?id=' . $commentId, $page); | ||
} | ||
|
||
/** @test */ | ||
public function it_doesnt_show_edit_comment_button_for_not_logged_in_user() | ||
{ | ||
$commentId = NewsCommentFactory::create( | ||
self::$pdo, | ||
$this->newsId, | ||
$this->userId, | ||
'Nice article!' | ||
); | ||
|
||
$page = $this->visitPage( | ||
__DIR__ . '/../../shownieuws.php', | ||
['id' => $this->newsId] | ||
); | ||
|
||
$this->assertNotContains('nieuwsEdit.php?id=' . $commentId, $page); | ||
} | ||
|
||
/** @test */ | ||
public function it_doesnt_show_edit_comment_button_for_other_users_comments() | ||
{ | ||
$this->login(); | ||
|
||
$commentId = NewsCommentFactory::create( | ||
self::$pdo, | ||
$this->newsId, | ||
$this->userId, | ||
'Nice article!' | ||
); | ||
|
||
$page = $this->visitPage( | ||
__DIR__ . '/../../shownieuws.php', | ||
['id' => $this->newsId] | ||
); | ||
|
||
$this->assertNotContains('nieuwsEdit.php?id=' . $commentId, $page); | ||
} | ||
} |
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