Permalink
Browse files

Basic confirmation implementation and tests

  • Loading branch information...
Deepwinter
Deepwinter committed Jan 19, 2012
1 parent 1769a07 commit 9ddcaf89af127ad4c09fb77e1c359fd2d3162614
Showing with 133 additions and 4 deletions.
  1. +44 −3 classes/confirmation.php
  2. +38 −0 classes/model/confirmation.php
  3. +9 −0 i18n/en.php
  4. +2 −1 sql/confirmations.sql
  5. +33 −0 tests/classes/ConfirmationTest.php
  6. +7 −0 tests/phpunit.xml
View
@@ -1,10 +1,51 @@
<?
-Class confirmation {
+Class Confirmation {
+
+ private $type = NULL;
+ private $model = NULL;
+
+ private $controller;
+ private $action;
+ private $arguments;
/*
- * Sends an email with a link that calls action 'confirm' on the class matching 'confirmation name'
+ * Sends an email with a link that calls controller/action/arguments when the confirmation is accepts
*/
- public static function email($emailAddress, $confirmationName){
+ public function __construct($type, $email, $controller, $action, array $arguments=NULL){
+ $this->type = $type;
+ $this->email = $email;
+ $this->controller = $controller;
+ $this->action = $action;
+ $this->arguments = $arguments;
+
+ $this->model= $this->createConfirmationModel();
+ }
+
+ public function send(){
+ mail($this->email, $this->createMessageSubject(), $this->createMessageBody());
+ }
+
+ public function createMessageBody(){
+ $body = i18n::get('confirmation.'.$this->type.'.body');
+ $confirmationLink = url::base('confirmation/'.$this->model->confirmation_id, 'http');
+ return str_replace(':confirmationLink', $confirmationLink, $body);
+ }
+
+ public function createMessageSubject(){
+ return i18n::get('confirmation.'.$this->type.'.subject');
}
+
+ private function createConfirmationModel(){
+ $confirmation = ORM::Factory('confirmation');
+ $confirmation->email = $this->email;
+ $confirmation->controller = $this->controller;
+ $confirmation->action = $this->action;
+ if($this->arguments){
+ $confirmation->arguments = json_encode($this->arguments);
+ }
+ $confirmation->save();
+ return $confirmation;
+ }
+
}
@@ -0,0 +1,38 @@
+<?
+
+Class Model_Confirmation extends ORM{
+
+
+
+ public function save(Validation $validation = NULL){
+ if(!$this->loaded()){
+ //we are inserting
+ $this->confirmation_id = $this->rand_str();
+ }
+ parent::save($validation);
+ }
+
+ // Generate a random character string
+ private static function rand_str($length = 32, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890')
+ {
+ // Length of character list
+ $chars_length = (strlen($chars) - 1);
+
+ // Start our string
+ $string = $chars{rand(0, $chars_length)};
+
+ // Generate random string
+ for ($i = 1; $i < $length; $i = strlen($string))
+ {
+ // Grab a random character from our list
+ $r = $chars{rand(0, $chars_length)};
+
+ // Make sure the same two characters don't appear next to each other
+ if ($r != $string{$i - 1}) $string .= $r;
+ }
+
+ // Return the string
+ return $string;
+ }
+
+}
View
@@ -0,0 +1,9 @@
+<?
+
+$lang = array(
+ 'confirmation.default.subject' => 'Confirm your information',
+ 'confirmation.default.body' => "Clink the link to confirm your information
+ :link",
+ );
+
+return $lang;
View
@@ -1,9 +1,10 @@
CREATE TABLE `confirmations` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`confirmation_id` varchar(100) CHARACTER SET latin1 NOT NULL,
+ `email` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`controller` varchar(100) CHARACTER SET latin1 NOT NULL,
`action` varchar(100) CHARACTER SET latin1 NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`timeouthours` int(11) NOT NULL,
PRIMARY KEY (`id`)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
@@ -0,0 +1,33 @@
+<?
+
+require_once 'PHPUnit/Framework.php';
+Class ConfirmationTest extends PHPUnit_Framework_TestCase
+ {
+ public function setUp(){
+ $_SERVER['SERVER_NAME'] = 'www.test.com';
+ }
+
+ public function testConstructConfirmation(){
+ $confirmation = new Confirmation('createUser', 'test@test.com', 'controller', 'action', array('id', 'id2'));
+ $this->assertNotNull($confirmation);
+ return $confirmation;
+ }
+
+ /**
+ * @depends testConstructConfirmation
+ */
+ public function testCreateMessageSubject($confirmation){
+ $subject = $confirmation->createMessageSubject();
+ $this->assertNotNull($subject);
+ }
+
+ /**
+ * @depends testConstructConfirmation
+ */
+ public function testCreateMessageBody($confirmation){
+ $body = $confirmation->createMessageBody();
+ $this->assertNotNull($body);
+
+ }
+ }
+
View
@@ -0,0 +1,7 @@
+<phpunit colors="true" bootstrap="testindex.php">
+ <testsuites>
+ <testsuite name="Tests">
+ <directory>modules/confirmation/tests/</directory>
+ </testsuite>
+ </testsuites>
+</phpunit>

0 comments on commit 9ddcaf8

Please sign in to comment.