Skip to content

Commit

Permalink
Added test coverage to show news page
Browse files Browse the repository at this point in the history
Added test coverage: #9
  • Loading branch information
Markkaz committed Jul 7, 2021
1 parent 510973e commit b597c65
Show file tree
Hide file tree
Showing 4 changed files with 273 additions and 85 deletions.
149 changes: 70 additions & 79 deletions shownieuws.php
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');
}
22 changes: 22 additions & 0 deletions tests/Factories/NewsCommentFactory.php
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();
}
}
179 changes: 179 additions & 0 deletions tests/Pages/ShowNewsTest.php
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);
}
}
8 changes: 2 additions & 6 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use PHPUnit\Framework\TestCase as BaseTestCase;
use Tests\Factories\UserFactory;
use Tests\Pages\IndexTest;

class TestCase extends BaseTestCase
{
Expand Down Expand Up @@ -45,11 +44,6 @@ protected function setUp()
$this->emptyTables();
}

protected function tearDown()
{
session_abort();
}

protected static function createDatabaseConnection()
{
self::$pdo = new \PDO('mysql:host=localhost;dbname=spelcodes', 'homestead', 'secret');
Expand Down Expand Up @@ -87,10 +81,12 @@ protected function visitPage($pagePath, array $get = [])
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$_GET = $get;

session_abort();
ob_start();
include $pagePath;
$page = ob_get_contents();
ob_end_clean();
session_abort();
return $page;
}

Expand Down

0 comments on commit b597c65

Please sign in to comment.