From d898efd8b4d291a26cfb7d6494d244de279a4679 Mon Sep 17 00:00:00 2001 From: Alexander Yakovlev Date: Sat, 12 Nov 2016 19:27:24 +0700 Subject: [PATCH] Proper period check --- application/classes/Model/Task.php | 28 +++++++++++++++++-------- application/classes/Task/Prepare.php | 2 +- application/tests/CoursesTest.php | 31 ++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/application/classes/Model/Task.php b/application/classes/Model/Task.php index 1e090a1..698b650 100644 --- a/application/classes/Model/Task.php +++ b/application/classes/Model/Task.php @@ -139,9 +139,13 @@ class Model_Task extends ORM { * Get last sent or prepared letter and check if it's time to send another one. * @return bool **/ - public static function check_period($client_id, $letters, $period) + public static function check_period($client_id, $letters, $period = NULL) { - $query = DB::select('date') + if (is_null($period) or $period === 0) + { + return TRUE; + } + $query = DB::select(array(DB::expr('COUNT(*)'), 'cnt')) ->from('tasks'); if (is_array($letters)) { @@ -151,15 +155,21 @@ class Model_Task extends ORM { { $query = $query->where('letter_id', '=', $letters); } - $check = NULL; + $query = $query + ->and_where('status', 'IN', [self::STATUS_SENT, self::STATUS_PENDING]) + ->and_where('client_id', '=', $client_id); + + $check = $query->execute()->get('cnt'); + if ($check === '0') // no letters at all + { + return TRUE; + } + $check = $query - ->and_where('status', '=', self::STATUS_SENT) - ->or_where('status', '=', self::STATUS_PENDING) - ->and_where('client_id', '=', $client_id) - ->and_where(DB::expr('DATEDIFF(CURDATE(), `date`)'), '=', $period) + ->and_where(DB::expr('DATEDIFF(CURDATE(), `date`)'), '>=', $period) ->execute() - ->get('date'); - return is_null($check); + ->get('cnt'); + return ( $check !== '0'); } /** diff --git a/application/classes/Task/Prepare.php b/application/classes/Task/Prepare.php index e74f483..f2853d8 100644 --- a/application/classes/Task/Prepare.php +++ b/application/classes/Task/Prepare.php @@ -48,7 +48,7 @@ class Task_Prepare extends Minion_Task Model_Task::prepare($client_id, $letter); } } else { - echo "Letter won't be prepared because it's too early to send it."; + echo "Letter won't be prepared because it's too early to send it.\n"; } } diff --git a/application/tests/CoursesTest.php b/application/tests/CoursesTest.php index b5d63ca..2f9a8e4 100644 --- a/application/tests/CoursesTest.php +++ b/application/tests/CoursesTest.php @@ -35,6 +35,37 @@ class CoursesTest extends Unittest_Database_TestCase $this->assertEquals(Model_Task::STATUS_PENDING, $status); } + /** + * @group Mail + * Tests that letter shouldn't be prepared if it's too early to send it. + **/ + function test_prepare_timeout() + { + DB::delete('tasks') + ->where('letter_id', '=', 1) + ->and_where('client_id','=',1) + ->execute(); + DB::insert('tasks', array('letter_id', 'client_id', 'date', 'status')) + ->values(array(1,1,date('Y-m-d'), Model_Task::STATUS_PENDING)) + ->execute(); + $check = Model_Task::check_period(1, 1, NULL); + $this->assertEquals(TRUE, $check); + $check = Model_Task::check_period(1, 1, 1); + $this->assertEquals(FALSE, $check); + $check = Model_Task::check_period(1, 1, 0); + $this->assertEquals(TRUE, $check); + + DB::delete('tasks') + ->where('letter_id', '=', 1) + ->and_where('client_id','=',1) + ->execute(); + DB::insert('tasks', array('letter_id', 'client_id', 'date', 'status')) + ->values(array(1,1,date('Y-m-d', strtotime("-1 days")), Model_Task::STATUS_PENDING)) + ->execute(); + $check = Model_Task::check_period(1, 1, 1); + $this->assertEquals(TRUE, $check); + } + function test_send_course() { $status = DB::select('status')