Skip to content

Commit

Permalink
Added test coverage for contact form
Browse files Browse the repository at this point in the history
Related to #9
Found issues: #21, #22
  • Loading branch information
Markkaz committed Jul 7, 2021
1 parent 5b9633c commit 0ad410b
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 47 deletions.
87 changes: 40 additions & 47 deletions contact.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,40 @@
<?php
error_reporting(E_ALL);
session_start();

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

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

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

/*Classes initialiseren */
$cUser = new User();
$cTPL = new Template('Templates/main.tpl');

/* Pagina goed weergeven */
include('Includes/login.php');

/* Controleren of het formulier is verzonden */
if(isset($_POST['titel']))
{
$sQuery = "INSERT INTO mail (mailid, titel, bericht, email, gelezen)
VALUES ('', '" . add($_POST['titel']) . "', '" . add($_POST['bericht']) . "',
'" . add($_POST['email']) . "', 0);";
if(mysql_query($sQuery))
{
$cTPL -> setPlace('TITEL', 'Email verzonden');
$cTPL -> setPlace('CONTENT', 'Je email is met succes verzonden. Wij proberen zo snel mogelijk reactie te geven');
}
else
{
$cTPL -> setPlace('TITEL', 'Fout met database');
$cTPL -> setPlace('CONTENT', 'Door een fout met de database is je email niet verzonden.');
}
}
else
{
$cTPL -> setPlace('TITEL', 'Contact');
$cTPL -> setFile('CONTENT', 'Templates/contact.tpl');
}

$cTPL -> show();
?>
<?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/slashes.php');
include_once('Includes/connect.php');

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

/*Classes initialiseren */
$cUser = new User();
$cTPL = new Template('Templates/main.tpl');

/* Pagina goed weergeven */
include('Includes/login.php');

/* Controleren of het formulier is verzonden */
if (isset($_POST['titel'])) {
$sQuery = "INSERT INTO mail (mailid, titel, bericht, email, gelezen)
VALUES ('', '" . add($_POST['titel']) . "', '" . add($_POST['bericht']) . "',
'" . add($_POST['email']) . "', 0);";
if (mysql_query($sQuery)) {
$cTPL->setPlace('TITEL', 'Email verzonden');
$cTPL->setPlace('CONTENT', 'Je email is met succes verzonden. Wij proberen zo snel mogelijk reactie te geven');
} else {
$cTPL->setPlace('TITEL', 'Fout met database');
$cTPL->setPlace('CONTENT', 'Door een fout met de database is je email niet verzonden.');
}
} else {
$cTPL->setPlace('TITEL', 'Contact');
$cTPL->setFile('CONTENT', 'Templates/contact.tpl');
}

$cTPL->show();
112 changes: 112 additions & 0 deletions tests/Pages/ContactTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace Tests\Pages;

use Tests\TestCase;

class ContactTest extends TestCase
{
/** @test */
public function it_shows_contact_form()
{
$page = $this->visitPage(__DIR__ . '/../../contact.php');

$this->assertContains(
'Let op: ALs je een vraag hebt, zet er dan een email adres bij. Anders kunnen wij niet antwoorden:',
$page
);
}

/** @test */
public function it_stores_a_valid_email()
{
$page = $this->visitPage(
__DIR__ . '/../../contact.php',
[],
[
'titel' => 'Hello!',
'email' => '[email protected]',
'bericht' => 'Hello, from me!'
]
);

$this->assertContains('Email verzonden', $page);
$this->assertDatabaseHas('mail', [
'titel' => 'Hello!',
'email' => '[email protected]',
'bericht' => 'Hello, from me!',
'gelezen' => 0
]);
}

/** @test */
public function it_shows_contact_form_when_no_title_was_provided()
{
$page = $this->visitPage(
__DIR__ . '/../../contact.php',
[],
[
'email' => '[email protected]',
'bericht' => 'Hello, from me!'
]
);

$this->assertContains(
'Let op: ALs je een vraag hebt, zet er dan een email adres bij. Anders kunnen wij niet antwoorden:',
$page
);
$this->assertDatabaseMissing('mail', [
'titel' => '',
'email' => '[email protected]',
'bericht' => 'Hello, from me!',
'gelezen' => 0
]);
}

/** @test */
public function it_shows_contact_form_when_no_email_was_provided()
{
$page = $this->visitPage(
__DIR__ . '/../../contact.php',
[],
[
'titel' => 'Hello!',
'bericht' => 'Hello, from me!'
]
);

$this->assertContains(
'Let op: ALs je een vraag hebt, zet er dan een email adres bij. Anders kunnen wij niet antwoorden:',
$page
);
$this->assertDatabaseMissing('mail', [
'titel' => 'Hello!',
'bericht' => 'Hello, from me!',
'gelezen' => 0
]);
}

/** @test */
public function it_shows_contact_form_when_no_body_was_provided()
{
$page = $this->visitPage(
__DIR__ . '/../../contact.php',
[],
[
'titel' => 'Hello!',
'email' => '[email protected]'
]
);

$this->assertContains(
'Let op: ALs je een vraag hebt, zet er dan een email adres bij. Anders kunnen wij niet antwoorden:',
$page
);
$this->assertDatabaseMissing('mail', [
'titel' => 'Hello!',
'email' => '[email protected]',
'bericht' => '',
'gelezen' => 0
]);
}
}

0 comments on commit 0ad410b

Please sign in to comment.