Permalink
Browse files

Added Unit tests and documentation for the alerts feature

Signed-off-by: Linda Kamau <kamaulynder@gmail.com>
  • Loading branch information...
1 parent 017ef13 commit c6ef8ff9691047cfa057872d75b03ca230c739d3 @kamaulynder kamaulynder committed Nov 23, 2011
@@ -15,9 +15,6 @@
class Alerts_Controller extends Main_Controller {
- const MOBILE_ALERT = 1;
- const EMAIL_ALERT = 2;
-
public function __construct()
{
parent::__construct();
@@ -109,25 +106,24 @@ public function index()
// If there is a post and $_POST is not empty
if ($post = $this->input->post())
{
- $alerts = new Alert_Model();
- if ($alerts->validate($post))
+ $alert_orm = new Alert_Model();
+ if ($alert_orm->validate($post))
{
- // Yes! everything is valid
- // Save alert and send out confirmation code
+ // Yes! everything is valid
+ // Save alert and send out confirmation code
if ( ! empty($post->alert_mobile))
{
- alert::_send_mobile_alert($post);
+ alert::_send_mobile_alert($post, $alert_orm);
+ $this->session->set('alert_mobile', $post->alert_mobile);
}
if ( ! empty($post->alert_email))
{
- alert::_send_email_alert($post);
+ alert::_send_email_alert($post, $alert_orm);
+ $this->session->set('alert_email', $post->alert_email);
}
- $this->session->set('alert_mobile', $post->alert_mobile);
- $this->session->set('alert_email', $post->alert_email);
-
url::redirect('alerts/confirm');
}
// No! We have validation errors, we need to show the form again, with the errors
@@ -14,16 +14,21 @@ class alert_Core {
const MOBILE_ALERT = 1;
const EMAIL_ALERT = 2;
- public static function _send_mobile_alert($post)
+ /**
+ * Sends an alert to a mobile phone
+ *
+ * @param Validation_Core $post
+ * @param Alert_Model $alert
+ * @return bool
+ */
+ public static function _send_mobile_alert($post, $alert)
{
- // For Mobile Alerts, Confirmation Code
- $alert_mobile = $post->alert_mobile;
- $alert_lon = $post->alert_lon;
- $alert_lat = $post->alert_lat;
- $alert_radius = $post->alert_radius;
- $alert_confirmed = $post->alert_confirmed;
-
- // Should be 6 distinct characters
+ if ( ! $post instanceof Validation_Core AND !$alert instanceof Alert_Model)
+ {
+ throw new Kohana_Exception('Invalid parameter types');
+ }
+
+ // Should be 8 distinct characters
$alert_code = text::random('distinct', 8);
$settings = ORM::factory('settings', 1);
@@ -52,16 +57,11 @@ public static function _send_mobile_alert($post)
$message = Kohana::lang('ui_admin.confirmation_code').$alert_code
.'.'.Kohana::lang('ui_admin.not_case_sensitive');
- if (sms::send($alert_mobile, $sms_from, $message) === true)
+ if (sms::send($post->alert_mobile, $sms_from, $message) === true)
{
- $alert = ORM::factory('alert');
$alert->alert_type = self::MOBILE_ALERT;
- $alert->alert_recipient = $alert_mobile;
+ $alert->alert_recipient = $post->alert_mobile;
$alert->alert_code = $alert_code;
- $alert->alert_lon = $alert_lon;
- $alert->alert_lat = $alert_lat;
- $alert->alert_radius = $alert_radius;
- $alert->alert_confirmed = $alert_confirmed;
if (isset($_SESSION['auth_user']))
{
$alert->user_id = $_SESSION['auth_user']->id;
@@ -78,16 +78,20 @@ public static function _send_mobile_alert($post)
/**
* Sends an email alert
+ *
+ * @param Validation_Core $post
+ * @param Alert_Model $alert
+ * @return bool
*/
- public static function _send_email_alert($post)
+ public static function _send_email_alert($post, $alert)
{
+ if ( ! $post instanceof Validation_Core AND !$alert instanceof Alert_Model)
+ {
+ throw new Kohana_Exception('Invalid parameter types');
+ }
+
// Email Alerts, Confirmation Code
$alert_email = $post->alert_email;
- $alert_lon = $post->alert_lon;
- $alert_lat = $post->alert_lat;
- $alert_radius = $post->alert_radius;
- $alert_confirmed = $post->alert_confirmed;
-
$alert_code = text::random('alnum', 20);
$settings = kohana::config('settings');
@@ -105,14 +109,9 @@ public static function _send_email_alert($post)
if (email::send($to, $from, $subject, $message, TRUE) == 1)
{
- $alert = ORM::factory('alert');
$alert->alert_type = self::EMAIL_ALERT;
$alert->alert_recipient = $alert_email;
$alert->alert_code = $alert_code;
- $alert->alert_lon = $alert_lon;
- $alert->alert_lat = $alert_lat;
- $alert->alert_radius = $alert_radius;
- $alert->alert_confirmed = $alert_confirmed;
if (isset($_SESSION['auth_user']))
{
$alert->user_id = $_SESSION['auth_user']->id;
@@ -130,50 +129,72 @@ public static function _send_email_alert($post)
/**
* This handles sms alerts subscription via phone
- * @params alert,location (city) - required
- * @params distance, category - optional
+ *
+ * @param string $message_from Subscriber MSISDN (mobile phone number)
+ * @param string $message_description Message content
+ * @return bool
*/
- public function mobile_alerts_register($message_from, $message_description)
+ public static function mobile_alerts_register($message_from, $message_description)
{
+ // Preliminary validation
+ if (empty($message_from) OR empty($message_description))
+ {
+ // Log the error
+ Kohana::log('info', 'Insufficient data to proceed with subscription via mobile phone');
+
+ // Return
+ return FALSE;
+ }
- /**
- * Get the message details (location, category, distance)
- */
- $message_details = explode(" ",$message_description);
- $message = $message_details[1].",".Kohana::config('settings.default_country');
- $geocoder = map::geocode($message);
+ //Get the message details (location, category, distance)
+ $message_details = explode(" ",$message_description);
+ $message = $message_details[1].",".Kohana::config('settings.default_country');
+ $geocoder = map::geocode($message);
- /**
- * Generate alert code
- */
- $alert_code = text::random('distinct', 8);
+ // Generate alert code
+ $alert_code = text::random('distinct', 8);
+ // POST variable with items to save
+ $post = array(
+ 'alert_type'=> self::MOBILE_ALERT,
+ 'alert_mobile'=>$message_from,
+ 'alert_code'=>$alert_code,
+ 'alert_lon'=>$geocoder['lon'],
+ 'alert_lat'=>$geocoder['lat'],
+ 'alert_radius'=>'20',
+ 'alert_confirmed'=>'1'
+ );
+
+ // Create ORM object for the alert and validate
+ $alert_orm = new Alert_Model();
+ if ($alert_orm->validate($post))
+ {
+ return self::_send_mobile_alert($post, $alert_orm);
+ }
- /* POST variable with items to save */
-
- $post = array(
- 'alert_type'=> self::MOBILE_ALERT,
- 'alert_mobile'=>$message_from,
- 'alert_code'=>$alert_code,
- 'alert_lon'=>$geocoder['lon'],
- 'alert_lat'=>$geocoder['lat'],
- 'alert_radius'=>'20',
- 'alert_confirmed'=>'1'
- );
-
- //convert the array to object
- $p = (object) $post;
-
- /**
- * Save alert details
- */
- self::_send_mobile_alert($p);
+ return FALSE;
}
-
- public function mobile_alerts_unsubscribe($message_from, $message_description)
+ /**
+ * This handles unsubscription from alerts via the mobile phone
+ *
+ * @param string $message_from Phone number of subscriber
+ * @param string $message_description Message content
+ * @return bool
+ */
+ public static function mobile_alerts_unsubscribe($message_from, $message_description)
{
+ // Validate parameters
+
+ if (empty($message_from) OR empty($message_description))
+ {
+ // Log the error
+ Kohana::log('info', 'Cannot unsubscribe from alerts via the mobile phone - insufficient data');
+
+ // Return
+ return FALSE;
+ }
$settings = ORM::factory('settings', 1);
@@ -204,7 +225,7 @@ public function mobile_alerts_unsubscribe($message_from, $message_description)
if (sms::send($message_from, $sms_from, $message) === true)
{
// Fetch all alerts with the specified code
- $alerts = ORM::factory('alert')
+ $alerts = ORM::factory('alert')
->where('alert_recipient', $message_from)
->find_all();
@@ -216,14 +237,19 @@ public function mobile_alerts_unsubscribe($message_from, $message_description)
->delete_all();
$alert->delete();
-
}
- }
+ return TRUE;
+ }
+ return FALSE;
}
+ /* This handles saving alert categories that a subscriber has subscribed for
+ *
+ * @param $Alert_Model $alert
+ */
- private function _add_categories(Alert_Model $alert, $post)
+ private static function _add_categories(Alert_Model $alert, $post)
{
if (isset($post->alert_category))
{
@@ -242,6 +268,4 @@ private function _add_categories(Alert_Model $alert, $post)
}
}
-
-
}
Oops, something went wrong.

0 comments on commit c6ef8ff

Please sign in to comment.