![]() |
Xataface Calendar Module 0.1
Full Calendar for Xataface
|
00001 <?php 00002 import('PHPUnit.php'); 00003 00004 class modules_calendar_RepeatEventTest extends PHPUnit_TestCase { 00005 00006 private static $TABLENAME = 'xftest_calendar_events'; 00007 private $mod = null; 00008 00009 function modules_calendar_RepeatEventTest( $name = 'modules_calendar_RepeatEventTest'){ 00010 $this->PHPUnit_TestCase($name); 00011 00012 } 00013 00014 function setUp(){ 00015 00016 $this->mod = Dataface_ModuleTool::getInstance()->loadModule('modules_calendar'); 00017 self::q('drop table if exists `'.self::$TABLENAME.'` '); 00018 $this->mod->dropRepeatTable(); 00019 00020 Dataface_Table::setBasePath(self::$TABLENAME, dirname(__FILE__)); 00021 self::q(file_get_contents(Dataface_Table::getBasePath(self::$TABLENAME).'/tables/'.self::$TABLENAME.'/create.sql')); 00022 00023 00024 00025 } 00026 00027 00028 00029 00030 00031 function tearDown(){ 00032 00033 00034 00035 } 00036 00037 00038 function testRepeats(){ 00039 00040 $record = new Dataface_Record(self::$TABLENAME, array()); 00041 $record->setValues(array( 00042 'start_time'=> '2011-01-05 13:30', 00043 'end_time'=> '2011-01-05 1400', 00044 'room_id'=>1 00045 00046 00047 )); 00048 $res = $record->save(); 00049 if ( PEAR::isError($res) ){ 00050 throw new Exception($res->getMessage(), $res->getCode()); 00051 } 00052 $repeat = $this->mod->newRepeat($record); 00053 $this->assertEquals('start_time', $repeat->getStartAtt(), 'Start att'); 00054 $this->assertEquals('end_time', $repeat->getEndAtt(), 'End att'); 00055 $this->assertEquals('repeat_id', $repeat->getRepeatAtt(), 'Repeat att'); 00056 $this->assertEquals('booking_id', $repeat->getRepeatSeedAtt(), 'Repeat seed att'); 00057 $this->assertEquals('2011-01-05 13:30:00', $repeat->getSourceStart(), 'Start time'); 00058 $this->assertEquals('2011-01-05 14:00:00', $repeat->getSourceEnd(), 'End time'); 00059 $this->assertEquals(null,$repeat->getFrequency(), 'Frequency initialized'); 00060 $this->assertEquals( null,$repeat->getExpiryDate(), 'Expiry date initialized'); 00061 $this->assertEquals(null, $repeat->getStartDate(), 'Start date initialized'); 00062 $this->assertEquals(null, $repeat->getRepeatID(), 'Initialized repeat id'); 00063 $this->assertEquals(false, $repeat->isSaved(), 'Initialized isSaved()'); 00064 $this->assertEquals($record, $repeat->getSourceRecord(), 'Source record'); 00065 00066 00067 $ex = null; 00068 try { 00069 $repeat->saveRepeatInfo(); 00070 } catch (Exception $e){ 00071 $ex = $e; 00072 } 00073 $this->assertEquals('Exception', get_class($ex), 'Check for frequency exception on save'); 00074 if ( $ex ){ 00075 $this->assertEquals(modules_calendar_RepeatEvent::$EX_INVALID_FREQUENCY, $ex->getCode(), 'Attempt to save before setting frequency'); 00076 } 00077 00078 $repeat->setFrequency('Weekly'); 00079 $repeat->saveRepeatInfo(); 00080 $this->assertEquals('Weekly', $repeat->getFrequency(), 'Checking frequency after save'); 00081 00082 00083 $record2 = df_get_record_by_id($record->getId()); 00084 $this->assertEquals($record->val('booking_id'), $record2->val('booking_id'), '2nd record booking id'); 00085 $repeat2 = $this->mod->newRepeat($record2); 00086 $this->assertEquals($repeat->getRepeatID(), $repeat2->getRepeatID(), 'Repeat id of reloaded repeat'); 00087 $this->assertEquals('Weekly', $repeat2->getFrequency(), 'Checking frequency of reloaded repeat'); 00088 $this->assertEquals('2011-01-05 13:30:00', $repeat2->getStartDate(), 'Checking start date of reloaded repeat'); 00089 00090 $expectedExpiry = date( 00091 'Y-m-d H:i:s', 00092 strtotime( 00093 $repeat->getDefaultExpiryDifferential(), 00094 strtotime('2011-01-05 13:30:00') 00095 ) 00096 ); 00097 $this->assertEquals($expectedExpiry, $repeat2->getExpiryDate(), 'Checking expiry date of reloaded repeat'); 00098 00099 00100 // Now let's get to the nitty gritty. 00101 $repeatRecords = $repeat->getRepeatRecords(); 00102 00103 $this->assertEquals(0, count($repeatRecords), 'Number or repeat records before repeat added'); 00104 $repeatRecords2 = $repeat2->getRepeatRecords(); 00105 $this->assertEquals(0, count($repeatRecords2), 'Number or repeat records in duplicate before repeat added'); 00106 00107 00108 $errs = $repeat->fillRepeats(); 00109 $this->assertEquals(0, count($errs), 'Should be 0 errors in fillRepeats()'); 00110 $repeatRecords = $repeat->getRepeatRecords(); 00111 $this->assertEquals(13, count($repeatRecords), 'Number of repeat records before repeat added'); 00112 //echo df_utc_offset(); 00113 foreach ($repeatRecords as $rr){ 00114 //echo "here"; 00115 $this->assertEquals( 00116 date('H:i:s', strtotime($repeat->getSourceStart())), 00117 date('H:i:s', strtotime($rr->strval($repeat->getStartAtt()))), 00118 'Start time should be the same for repeat: '.$rr->getId() 00119 ); 00120 } 00121 00122 00123 00124 $errors = $repeat->clearRepeats(); 00125 $this->assertEquals(0, count($errors), 'Should be no errors after clearing repeats'); 00126 $repeatRecords = $repeat->getRepeatRecords(); 00127 $this->assertEquals(0, count($repeatRecords), 'Should be no repeats left after clearing repeats'); 00128 00129 $errs = $repeat->fillRepeats(); 00130 $this->assertEquals(0, count($errs), 'Should be 0 errors in fillRepeats() after clearing'); 00131 $repeatRecords = $repeat->getRepeatRecords(); 00132 $this->assertEquals(13, count($repeatRecords), 'Should be 13 repeats after repeat re-filled after clearing them'); 00133 //echo df_utc_offset(); 00134 foreach ($repeatRecords as $rr){ 00135 //echo "here"; 00136 $this->assertEquals( 00137 date('H:i:s', strtotime($repeat->getSourceStart())), 00138 date('H:i:s', strtotime($rr->strval($repeat->getStartAtt()))), 00139 'Start time should be the same for repeat: '.$rr->getId() 00140 ); 00141 } 00142 00143 00144 $errors = $repeat->clearRepeats(); 00145 $this->assertEquals(0, count($errors), 'Should be no errors after clearing repeats'); 00146 $repeatRecords = $repeat->getRepeatRecords(); 00147 $this->assertEquals(0, count($repeatRecords), 'Should be no repeats left after clearing repeats'); 00148 00149 00150 $repeat->setFrequency('Daily'); 00151 $errs = $repeat->fillRepeats(); 00152 $this->assertEquals(0, count($errs), 'Should be 0 errors in fillRepeats() after clearing'); 00153 $repeatRecords = $repeat->getRepeatRecords(); 00154 $this->assertEquals(90, count($repeatRecords), 'Should be 90 repeats after changing frequency to daily'); 00155 //echo df_utc_offset(); 00156 foreach ($repeatRecords as $rr){ 00157 //echo "here"; 00158 $this->assertEquals( 00159 date('H:i:s', strtotime($repeat->getSourceStart())), 00160 date('H:i:s', strtotime($rr->strval($repeat->getStartAtt()))), 00161 'Start time should be the same for repeat: '.$rr->getId() 00162 ); 00163 } 00164 00165 00166 00167 00168 $record3 = new Dataface_Record(self::$TABLENAME, array()); 00169 $record3->setValues(array( 00170 'start_time'=> '2011-01-05 18:30', 00171 'end_time'=> '2011-01-05 19:00', 00172 'room_id'=>1 00173 00174 00175 )); 00176 $res = $record3->save(); 00177 if ( PEAR::isError($res) ){ 00178 throw new Exception($res->getMessage(), $res->getCode()); 00179 } 00180 $repeat->setSourceRecord($record3); 00181 $this->assertEquals(false, $repeat->isSaved(), 'isSaved() should be false after changing source record to one with no repeat id.'); 00182 $this->assertEquals(null, $repeat->getFrequency(), 'frequency should be null after changing source record to one with no repeat yet.'); 00183 $this->assertEquals(null, $repeat->getStartDate(), 'start date should be null after changing the source record.'); 00184 $this->assertEquals(null, $repeat->getExpiryDate(), 'expiry date should be null after changing the source record.'); 00185 00186 $repeat->setFrequency('Daily'); 00187 $record3->setValue($repeat->getStartAtt(), date('Y-m-d H:i:s')); 00188 $repeat->setExpiryDate(date('Y-m-d H:i:s', strtotime('+5 days'))); 00189 00190 $errors = $repeat->commit(array(), 0, 0); 00191 $this->assertEquals(0, count($errors), 'There should be no errors on commit'); 00192 00193 $repeatID = $repeat->getRepeatID(); 00194 $this->assertTrue(@$repeatID, 'The repeat id should be set after commit'); 00195 $this->assertTrue(@$record3->val($repeat->getRepeatAtt()), 'The repeat id should be set in the record after commit also.'); 00196 $this->assertEquals($repeatID, $record3->val($repeat->getRepeatAtt()), 'The getRepeatID() should be the same as the repeatID returned from the record.'); 00197 00198 00199 $repeatRecords = $repeat->getRepeatRecords(); 00200 $this->assertEquals(5, count($repeatRecords), 'Should be 5 repeats after committing the repeat'); 00201 //echo df_utc_offset(); 00202 foreach ($repeatRecords as $rr){ 00203 //echo "here"; 00204 $this->assertEquals( 00205 date('H:i:s', strtotime($repeat->getSourceStart())), 00206 date('H:i:s', strtotime($rr->strval($repeat->getStartAtt()))), 00207 'Start time should be the same for repeat: '.$rr->getId() 00208 ); 00209 } 00210 00211 00212 00213 00214 00215 00216 00217 00218 00219 00220 00221 00222 00223 00224 00225 00226 } 00227 static function q($sql){ 00228 $res = mysql_query($sql, df_db()); 00229 if ( !$res ) throw new Exception(mysql_error(df_db())); 00230 return $res; 00231 } 00232 00233 00234 00235 00236 } 00237 00238 00239 // Add this test to the suite of tests to be run by the testrunner 00240 Dataface_ModuleTool::getInstance()->loadModule('modules_testrunner') 00241 ->addTest('modules_calendar_RepeatEventTest');