Browse Source

Prepare the code to test properly sendtomail.php

master
Sébastien Lucas 10 years ago
parent
commit
9753938a2e
3 changed files with 92 additions and 12 deletions
  1. +28
    -11
      sendtomail.php
  2. +8
    -1
      test/config_test.php
  3. +56
    -0
      test/mailTest.php

+ 28
- 11
sendtomail.php View File

@@ -4,22 +4,39 @@ require_once ("config.php");
require_once "resources/PHPMailer/class.phpmailer.php";
require_once "book.php";

if (is_null ($config['cops_mail_configuration']) ||
!is_array ($config['cops_mail_configuration']) ||
empty ($config['cops_mail_configuration']["smtp.host"]) ||
empty ($config['cops_mail_configuration']["address.from"])) {
echo "NOK. bad configuration of $config ['cops_mail_configuration']";
exit;
function checkConfiguration () {
global $config;

if (is_null ($config['cops_mail_configuration']) ||
!is_array ($config['cops_mail_configuration']) ||
empty ($config['cops_mail_configuration']["smtp.host"]) ||
empty ($config['cops_mail_configuration']["address.from"])) {
return "NOK. bad configuration.";
}
return False;
}

$idData = $_REQUEST["data"];
if (empty ($idData)) {
echo 'No data sent.';
function checkRequest ($idData, $emailDest) {
if (empty ($idData)) {
return 'No data sent.';
}
if (empty ($emailDest)) {
return 'No email sent.';
}
return False;
}

if (php_sapi_name() === 'cli') { return; }

if ($error = checkConfiguration ()) {
echo $error;
exit;
}

$idData = $_REQUEST["data"];
$emailDest = $_REQUEST["email"];
if (empty ($emailDest)) {
echo 'No email sent.';
if ($error = checkRequest ($idData, $emailDest)) {
echo $error;
exit;
}



+ 8
- 1
test/config_test.php View File

@@ -1,4 +1,11 @@
<?php

require_once (dirname(__FILE__) . "/../config_default.php");
$config['calibre_directory'] = dirname(__FILE__) . "/BaseWithSomeBooks/";
$config['calibre_directory'] = dirname(__FILE__) . "/BaseWithSomeBooks/";

$config['cops_mail_configuration'] = array( "smtp.host" => "smtp.free.fr",
"smtp.username" => "",
"smtp.password" => "",
"smtp.secure" => "",
"address.from" => "cops@slucas.fr"
);

+ 56
- 0
test/mailTest.php View File

@@ -0,0 +1,56 @@
<?php
/**
* COPS (Calibre OPDS PHP Server) test file
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Sébastien Lucas <sebastien@slucas.fr>
*/
require_once (dirname(__FILE__) . "/config_test.php");
require_once (dirname(__FILE__) . "/../book.php");
require_once (dirname(__FILE__) . "/../sendtomail.php");
class MailTest extends PHPUnit_Framework_TestCase
{
public function testCheckConfiguration () {
global $config;
$this->assertFalse(checkConfiguration ());
}
public function testCheckConfigurationNull () {
global $config;
$config['cops_mail_configuration'] = NULL;
$this->assertStringStartsWith("NOK", checkConfiguration ());
}
public function testCheckConfigurationNotArray () {
global $config;
$config['cops_mail_configuration'] = "Test";
$this->assertStringStartsWith("NOK", checkConfiguration ());
}
public function testCheckConfigurationSmtpEmpty () {
global $config;
$config['cops_mail_configuration']["smtp.host"] = "";
$this->assertStringStartsWith("NOK", checkConfiguration ());
}
public function testCheckConfigurationEmailEmpty () {
global $config;
$config['cops_mail_configuration']["address.from"] = "";
$this->assertStringStartsWith("NOK", checkConfiguration ());
}
public function testCheckConfigurationEmailNotValid () {
global $config;
$config['cops_mail_configuration']["address.from"] = "a";
$this->markTestIncomplete();
}
}

Loading…
Cancel
Save