Ticket #1184: piwik-dev1 (#1184).patch

File piwik-dev1 (#1184).patch, 61.4 KB (added by JulienM, 22 months ago)
  • tests/core/ScheduledTask.test.php

     
     1<?php 
     2if(!defined("PIWIK_PATH_TEST_TO_ROOT")) { 
     3        define('PIWIK_PATH_TEST_TO_ROOT', getcwd().'/../..'); 
     4} 
     5if(!defined('PIWIK_CONFIG_TEST_INCLUDED')) 
     6{ 
     7        require_once PIWIK_PATH_TEST_TO_ROOT . "/tests/config_test.php"; 
     8} 
     9 
     10require_once 'ScheduledTask.php'; 
     11 
     12class Test_Piwik_ScheduledTask extends UnitTestCase 
     13{ 
     14        public function test_getClassName() 
     15        { 
     16                $scheduledTask = new Piwik_ScheduledTask ( "className", null, null ); 
     17                $className = $scheduledTask->getClassName(); 
     18                $this->assertTrue( is_string($className) && !empty($className) ); 
     19        } 
     20         
     21        public function test_getMethodName() 
     22        { 
     23                $scheduledTask = new Piwik_ScheduledTask ( null, "methodname", null ); 
     24                $methodName = $scheduledTask->getMethodName(); 
     25                $this->assertTrue( is_string($methodName) && !empty($methodName) ); 
     26        } 
     27         
     28        public function test_getScheduledTime() 
     29        { 
     30                $scheduledTask = new Piwik_ScheduledTask ( null, null, new Piwik_ScheduledTime_Hourly() ); 
     31                $scheduledTime = $scheduledTask->getScheduledTime(); 
     32                $this->assertTrue( get_class($scheduledTime) == "Piwik_ScheduledTime_Hourly" ); 
     33        }        
     34} 
     35 No newline at end of file 
  • tests/core/ScheduledTime.test.php

     
     1<?php 
     2if(!defined("PIWIK_PATH_TEST_TO_ROOT")) { 
     3        define('PIWIK_PATH_TEST_TO_ROOT', getcwd().'/../..'); 
     4} 
     5if(!defined('PIWIK_CONFIG_TEST_INCLUDED')) 
     6{ 
     7        require_once PIWIK_PATH_TEST_TO_ROOT . "/tests/config_test.php"; 
     8} 
     9 
     10require_once 'ScheduledTime.php'; 
     11require_once 'ScheduledTime/Hourly.php'; 
     12require_once 'ScheduledTime/Daily.php'; 
     13require_once 'ScheduledTime/Weekly.php'; 
     14require_once 'ScheduledTime/Monthly.php'; 
     15 
     16Mock::generatePartial('Piwik_ScheduledTime_Hourly', 'Piwik_ScheduledTime_Hourly_Test', array('getTime')); 
     17Mock::generatePartial('Piwik_ScheduledTime_Daily', 'Piwik_ScheduledTime_Daily_Test', array('getTime')); 
     18Mock::generatePartial('Piwik_ScheduledTime_Weekly', 'Piwik_ScheduledTime_Weekly_Test', array('getTime')); 
     19Mock::generatePartial('Piwik_ScheduledTime_Monthly', 'Piwik_ScheduledTime_Monthly_Test', array('getTime')); 
     20 
     21class Test_Piwik_ScheduledTime extends UnitTestCase 
     22{ 
     23        private $JANUARY_01_1971_09_00_00; 
     24        private $JANUARY_01_1971_09_10_00; 
     25        private $JANUARY_01_1971_10_00_00; 
     26        private $JANUARY_01_1971_10_10_00; 
     27        private $JANUARY_01_1971_11_00_00; 
     28        private $JANUARY_01_1971_12_00_00; 
     29        private $JANUARY_01_1971_12_10_00; 
     30         
     31        private $JANUARY_02_1971_09_00_00; 
     32        private $JANUARY_02_1971_09_10_00; 
     33        private $JANUARY_02_1971_12_10_00; 
     34         
     35        private $JANUARY_03_1971_12_10_00; 
     36         
     37        private $JANUARY_04_1971_09_00_00; 
     38        private $JANUARY_04_1971_09_10_00; 
     39        private $JANUARY_04_1971_12_10_00; 
     40         
     41        private $JANUARY_05_1971_09_00_00; 
     42        private $JANUARY_05_1971_09_10_00; 
     43        private $JANUARY_05_1971_12_00_00; 
     44         
     45        private $JANUARY_08_1971_09_00_00; 
     46        private $JANUARY_08_1971_09_10_00; 
     47         
     48        private $JANUARY_09_1971_09_00_00; 
     49        private $JANUARY_09_1971_09_10_00; 
     50         
     51        private $JANUARY_11_1971_09_00_00; 
     52        private $JANUARY_11_1971_09_10_00; 
     53         
     54        private $JANUARY_12_1971_09_00_00; 
     55        private $JANUARY_18_1971_09_00_00; 
     56        private $JANUARY_19_1971_09_00_00; 
     57         
     58        private $JANUARY_25_1971_09_00_00; 
     59        private $JANUARY_26_1971_09_00_00; 
     60         
     61        private $FEBRUARY_01_1971_09_00_00; 
     62        private $FEBRUARY_02_1971_09_00_00; 
     63        private $FEBRUARY_03_1971_09_00_00; 
     64        private $FEBRUARY_03_1971_12_10_00; 
     65        private $FEBRUARY_04_1971_09_00_00; 
     66        private $FEBRUARY_04_1971_12_10_00; 
     67        private $FEBRUARY_05_1971_09_00_00; 
     68        private $FEBRUARY_08_1971_09_00_00; 
     69        private $FEBRUARY_15_1971_09_00_00; 
     70        private $FEBRUARY_16_1971_09_00_00; 
     71        private $FEBRUARY_17_1971_09_00_00; 
     72         
     73        private $MARCH_05_1971_09_00_00; 
     74         
     75        function __construct( $title = '') 
     76        { 
     77                $this->JANUARY_01_1971_09_00_00 = mktime(9,00,00,1,1,1971); 
     78                $this->JANUARY_01_1971_09_10_00 = mktime(9,10,00,1,1,1971); 
     79                $this->JANUARY_01_1971_10_00_00 = mktime(10,00,00,1,1,1971); 
     80                $this->JANUARY_01_1971_10_10_00 = mktime(10,10,00,1,1,1971); 
     81                $this->JANUARY_01_1971_11_00_00 = mktime(11,00,00,1,1,1971); 
     82                $this->JANUARY_01_1971_12_00_00 = mktime(12,00,00,1,1,1971); 
     83                $this->JANUARY_01_1971_12_10_00 = mktime(12,10,00,1,1,1971); 
     84                 
     85                $this->JANUARY_02_1971_09_00_00 = mktime(9,00,00,1,2,1971); 
     86                $this->JANUARY_02_1971_09_10_00 = mktime(9,10,00,1,2,1971); 
     87                $this->JANUARY_02_1971_12_10_00 = mktime(12,10,00,1,2,1971); 
     88                 
     89                $this->JANUARY_03_1971_12_10_00 = mktime(12,10,00,1,3,1971); 
     90                 
     91                $this->JANUARY_04_1971_09_00_00 = mktime(9,00,00,1,4,1971); 
     92                $this->JANUARY_04_1971_09_10_00 = mktime(9,10,00,1,4,1971); 
     93                $this->JANUARY_04_1971_12_10_00 = mktime(12,10,00,1,4,1971); 
     94                 
     95                $this->JANUARY_05_1971_09_00_00 = mktime(9,00,00,1,5,1971); 
     96                $this->JANUARY_05_1971_09_10_00 = mktime(9,10,00,1,5,1971); 
     97                $this->JANUARY_05_1971_12_00_00 = mktime(12,00,00,1,5,1971); 
     98                 
     99                $this->JANUARY_08_1971_09_00_00 = mktime(9,00,00,1,8,1971); 
     100                $this->JANUARY_08_1971_09_10_00 = mktime(9,10,00,1,8,1971); 
     101                 
     102                $this->JANUARY_09_1971_09_00_00 = mktime(9,00,00,1,9,1971); 
     103                $this->JANUARY_09_1971_09_10_00 = mktime(9,10,00,1,9,1971); 
     104                 
     105                $this->JANUARY_11_1971_09_00_00 = mktime(9,00,00,1,11,1971); 
     106                $this->JANUARY_11_1971_09_10_00 = mktime(9,10,00,1,11,1971); 
     107                 
     108                $this->JANUARY_12_1971_09_00_00 = mktime(9,00,00,1,12,1971); 
     109                $this->JANUARY_18_1971_09_00_00 = mktime(9,00,00,1,18,1971); 
     110                $this->JANUARY_19_1971_09_00_00 = mktime(9,00,00,1,19,1971); 
     111                 
     112                $this->JANUARY_25_1971_09_00_00 = mktime(9,00,00,1,25,1971); 
     113                $this->JANUARY_26_1971_09_00_00 = mktime(9,00,00,1,26,1971); 
     114 
     115                $this->FEBRUARY_01_1971_09_00_00 = mktime(9,00,00,2,1,1971);     
     116                $this->FEBRUARY_02_1971_09_00_00 = mktime(9,00,00,2,2,1971);     
     117                $this->FEBRUARY_03_1971_09_00_00 = mktime(9,00,00,2,3,1971);     
     118                $this->FEBRUARY_03_1971_12_10_00 = mktime(12,10,00,2,3,1971);    
     119                $this->FEBRUARY_04_1971_09_00_00 = mktime(9,00,00,2,4,1971);     
     120                $this->FEBRUARY_04_1971_12_10_00 = mktime(12,10,00,2,4,1971);    
     121                $this->FEBRUARY_05_1971_09_00_00 = mktime(9,00,00,2,5,1971);     
     122                $this->FEBRUARY_08_1971_09_00_00 = mktime(9,00,00,2,8,1971);     
     123                $this->FEBRUARY_15_1971_09_00_00 = mktime(9,00,00,2,15,1971);    
     124                $this->FEBRUARY_16_1971_09_00_00 = mktime(9,00,00,2,16,1971);    
     125                $this->FEBRUARY_17_1971_09_00_00 = mktime(9,00,00,2,17,1971);    
     126                 
     127                $this->MARCH_05_1971_09_00_00 = mktime(9,00,00,3,5,1971); 
     128 
     129                parent::__construct( $title ); 
     130        } 
     131         
     132        /* 
     133         * Tests forbidden call to setHour on Piwik_ScheduledTime_Hourly 
     134         */ 
     135        public function test_setHour_ScheduledTime_Hourly() 
     136        { 
     137                try { 
     138                        $hourlySchedule = new Piwik_ScheduledTime_Hourly(); 
     139                        $hourlySchedule->setHour(0); 
     140                        $this->fail("Exception not raised."); 
     141                } 
     142                catch (Exception $expected) { 
     143                        $this->pass(); 
     144                        return; 
     145                } 
     146        } 
     147 
     148        /* 
     149         * Tests invalid call to setHour on Piwik_ScheduledTime_Daily 
     150         */ 
     151        public function test_setHour_ScheduledTime_Daily_Negative() 
     152        { 
     153                try { 
     154                        $dailySchedule = new Piwik_ScheduledTime_Daily(); 
     155                        $dailySchedule->setHour(-1); 
     156                        $this->fail("Exception not raised."); 
     157                } 
     158                catch (Exception $expected) { 
     159                        $this->pass(); 
     160                        return; 
     161                } 
     162        } 
     163 
     164        /* 
     165         * Tests invalid call to setHour on Piwik_ScheduledTime_Daily 
     166         */ 
     167        public function test_setHour_ScheduledTime_Daily_Over_24() 
     168        { 
     169                try { 
     170                        $dailySchedule = new Piwik_ScheduledTime_Daily(); 
     171                        $dailySchedule->setHour(25); 
     172                        $this->fail("Exception not raised."); 
     173                } 
     174                catch (Exception $expected) { 
     175                        $this->pass(); 
     176                        return; 
     177                } 
     178        } 
     179         
     180        /* 
     181         * Tests invalid call to setHour on Piwik_ScheduledTime_Weekly 
     182         */ 
     183        public function test_setHour_ScheduledTime_Weekly_Negative() 
     184        { 
     185                try { 
     186                        $weeklySchedule = new Piwik_ScheduledTime_Weekly(); 
     187                        $weeklySchedule->setHour(-1); 
     188                        $this->fail("Exception not raised."); 
     189                } 
     190                catch (Exception $expected) { 
     191                        $this->pass(); 
     192                        return; 
     193                } 
     194        } 
     195 
     196        /* 
     197         * Tests invalid call to setHour on Piwik_ScheduledTime_Weekly 
     198         */ 
     199        public function test_setHour_ScheduledTime_Weekly_Over_24() 
     200        { 
     201                try { 
     202                        $weeklySchedule = new Piwik_ScheduledTime_Weekly(); 
     203                        $weeklySchedule->setHour(25); 
     204                        $this->fail("Exception not raised."); 
     205                } 
     206                catch (Exception $expected) { 
     207                        $this->pass(); 
     208                        return; 
     209                } 
     210        } 
     211         
     212        /* 
     213         * Tests invalid call to setHour on Piwik_ScheduledTime_Monthly 
     214         */ 
     215        public function test_setHour_ScheduledTime_Monthly_Negative() 
     216        { 
     217                try { 
     218                        $monthlySchedule = new Piwik_ScheduledTime_Monthly(); 
     219                        $monthlySchedule->setHour(-1); 
     220                        $this->fail("Exception not raised."); 
     221                } 
     222                catch (Exception $expected) { 
     223                        $this->pass(); 
     224                        return; 
     225                } 
     226        } 
     227 
     228        /* 
     229         * Tests invalid call to setHour on Piwik_ScheduledTime_Monthly 
     230         */ 
     231        public function test_setHour_ScheduledTime_Monthly_Over_24() 
     232        { 
     233                try { 
     234                        $monthlySchedule = new Piwik_ScheduledTime_Monthly(); 
     235                        $monthlySchedule->setHour(25); 
     236                        $this->fail("Exception not raised."); 
     237                } 
     238                catch (Exception $expected) { 
     239                        $this->pass(); 
     240                        return; 
     241                } 
     242        }        
     243 
     244        /* 
     245         * Tests forbidden call to setDay on Piwik_ScheduledTime_Hourly 
     246         */ 
     247        public function test_setDay_ScheduledTime_Hourly() 
     248        { 
     249                try { 
     250                        $hourlySchedule = new Piwik_ScheduledTime_Hourly(); 
     251                        $hourlySchedule->setDay(1); 
     252                        $this->fail("Exception not raised."); 
     253                } 
     254                catch (Exception $expected) { 
     255                        $this->pass(); 
     256                        return; 
     257                } 
     258        } 
     259         
     260        /* 
     261         * Tests forbidden call to setDay on Piwik_ScheduledTime_Daily 
     262         */ 
     263        public function test_setDay_ScheduledTime_Daily() 
     264        { 
     265                try { 
     266                        $dailySchedule = new Piwik_ScheduledTime_Daily(); 
     267                        $dailySchedule->setDay(1); 
     268                        $this->fail("Exception not raised."); 
     269                } 
     270                catch (Exception $expected) { 
     271                        $this->pass(); 
     272                        return; 
     273                } 
     274        } 
     275         
     276        /* 
     277         * Tests invalid call to setDay on Piwik_ScheduledTime_Weekly 
     278         */ 
     279        public function test_setDay_ScheduledTime_Weekly_Day_0() 
     280        { 
     281                try { 
     282                        $weeklySchedule = new Piwik_ScheduledTime_Weekly(); 
     283                        $weeklySchedule->setDay(0); 
     284                        $this->fail("Exception not raised."); 
     285                } 
     286                catch (Exception $expected) { 
     287                        $this->pass(); 
     288                        return; 
     289                } 
     290        } 
     291 
     292        /* 
     293         * Tests invalid call to setDay on Piwik_ScheduledTime_Weekly 
     294         */ 
     295        public function test_setDay_ScheduledTime_Weekly_Over_7() 
     296        { 
     297                try { 
     298                        $weeklySchedule = new Piwik_ScheduledTime_Weekly(); 
     299                        $weeklySchedule->setDay(8); 
     300                        $this->fail("Exception not raised."); 
     301                } 
     302                catch (Exception $expected) { 
     303                        $this->pass(); 
     304                        return; 
     305                } 
     306        } 
     307         
     308        /* 
     309         * Tests invalid call to setDay on Piwik_ScheduledTime_Monthly 
     310         */ 
     311        public function test_setDay_ScheduledTime_Monthly_Day_0() 
     312        { 
     313                try { 
     314                        $monthlySchedule = new Piwik_ScheduledTime_Monthly(); 
     315                        $monthlySchedule->setDay(0); 
     316                        $this->fail("Exception not raised."); 
     317                } 
     318                catch (Exception $expected) { 
     319                        $this->pass(); 
     320                        return; 
     321                } 
     322        } 
     323 
     324        /* 
     325         * Tests invalid call to setDay on Piwik_ScheduledTime_Monthly 
     326         */ 
     327        public function test_setDay_ScheduledTime_Monthly_Over_7() 
     328        { 
     329                try { 
     330                        $monthlySchedule = new Piwik_ScheduledTime_Monthly(); 
     331                        $monthlySchedule->setDay(8); 
     332                        $this->fail("Exception not raised."); 
     333                } 
     334                catch (Exception $expected) { 
     335                        $this->pass(); 
     336                        return; 
     337                } 
     338        } 
     339         
     340        /* 
     341         * Tests forbidden call to setWeek on Piwik_ScheduledTime_Hourly 
     342         */ 
     343        public function test_setWeek_ScheduledTime_Hourly() 
     344        { 
     345                try { 
     346                        $hourlySchedule = new Piwik_ScheduledTime_Hourly(); 
     347                        $hourlySchedule->setWeek(1); 
     348                        $this->fail("Exception not raised."); 
     349                } 
     350                catch (Exception $expected) { 
     351                        $this->pass(); 
     352                        return; 
     353                } 
     354        } 
     355         
     356        /* 
     357         * Tests forbidden call to setWeek on Piwik_ScheduledTime_Daily 
     358         */ 
     359        public function test_setWeek_ScheduledTime_Daily() 
     360        { 
     361                try { 
     362                        $dailySchedule = new Piwik_ScheduledTime_Daily(); 
     363                        $dailySchedule->setWeek(1); 
     364                        $this->fail("Exception not raised."); 
     365                } 
     366                catch (Exception $expected) { 
     367                        $this->pass(); 
     368                        return; 
     369                } 
     370        } 
     371         
     372        /* 
     373         * Tests forbidden call to setWeek on Piwik_ScheduledTime_Weekly 
     374         */ 
     375        public function test_setWeek_ScheduledTime_Weekly() 
     376        { 
     377                try { 
     378                        $weeklySchedule = new Piwik_ScheduledTime_Weekly(); 
     379                        $weeklySchedule->setWeek(1); 
     380                        $this->fail("Exception not raised."); 
     381                } 
     382                catch (Exception $expected) { 
     383                        $this->pass(); 
     384                        return; 
     385                } 
     386        }        
     387         
     388        /* 
     389         * Tests invalid call to setWeek on Piwik_ScheduledTime_Monthly 
     390         */ 
     391        public function test_setWeek_ScheduledTime_Monthly_Week_0() 
     392        { 
     393                try { 
     394                        $monthlySchedule = new Piwik_ScheduledTime_Monthly(); 
     395                        $monthlySchedule->setWeek(0); 
     396                        $this->fail("Exception not raised."); 
     397                } 
     398                catch (Exception $expected) { 
     399                        $this->pass(); 
     400                        return; 
     401                } 
     402        } 
     403 
     404        /* 
     405         * Tests invalid call to setWeek on Piwik_ScheduledTime_Monthly 
     406         */ 
     407        public function test_setWeek_ScheduledTime_Monthly_Over_4() 
     408        { 
     409                try { 
     410                        $monthlySchedule = new Piwik_ScheduledTime_Monthly(); 
     411                        $monthlySchedule->setWeek(5); 
     412                        $this->fail("Exception not raised."); 
     413                } 
     414                catch (Exception $expected) { 
     415                        $this->pass(); 
     416                        return; 
     417                } 
     418        }        
     419         
     420        /* 
     421         * Tests getRescheduledTime on Piwik_ScheduledTime_Hourly 
     422         * 
     423         */ 
     424        public function test_getRescheduledTime_Hourly() 
     425        { 
     426                 
     427                /* 
     428                 * Test 1 
     429                 * 
     430                 * Context : 
     431                 *  - Cron runs at January 1 1971 09:00:00 GMT 
     432                 *  - getRescheduledTime is called within the same second as the cron task 
     433                 * 
     434                 * Expected : 
     435                 *  getRescheduledTime returns January 1 1971 10:00:00 GMT 
     436                 */ 
     437                $hourlySchedule = new Piwik_ScheduledTime_Hourly_Test(); 
     438                $hourlySchedule->setReturnValue('getTime', $this->JANUARY_01_1971_09_00_00); 
     439                $this->assertEqual($hourlySchedule->getRescheduledTime(), $this->JANUARY_01_1971_10_00_00); 
     440 
     441                /* 
     442                 * Test 2 
     443                 * 
     444                 * Context : 
     445                 *  - Cron runs at January 1 1971 09:00:00 GMT 
     446                 *  - getRescheduledTime is called 10 minutes after the cron task to simulate a previous task 
     447                 * 
     448                 * Expected : 
     449                 *  getRescheduledTime returns January 1 1971 10:00:00 GMT 
     450                 */ 
     451                $hourlySchedule = new Piwik_ScheduledTime_Hourly_Test(); 
     452                $hourlySchedule->setReturnValue('getTime', $this->JANUARY_01_1971_09_10_00); 
     453                $this->assertEqual($hourlySchedule->getRescheduledTime(), $this->JANUARY_01_1971_10_00_00); 
     454 
     455                /* 
     456                 * Test 3 
     457                 * 
     458                 * Context : 
     459                 *  - Cron runs at January 1 1971 09:00:00 GMT 
     460                 *  - getRescheduledTime is called 1 hour and 10 minutes after the cron task to simulate a long previous task 
     461                 * 
     462                 * Expected : 
     463                 *  getRescheduledTime returns January 1 1971 11:00:00 GMT 
     464                 */ 
     465                $hourlySchedule = new Piwik_ScheduledTime_Hourly_Test(); 
     466                $hourlySchedule->setReturnValue('getTime', $this->JANUARY_01_1971_10_10_00); 
     467                $this->assertEqual($hourlySchedule->getRescheduledTime(), $this->JANUARY_01_1971_11_00_00); 
     468        } 
     469 
     470        /* 
     471         * Tests getRescheduledTime on Piwik_ScheduledTime_Daily with unspecified hour 
     472         * 
     473         */ 
     474        public function test_getRescheduledTime_Daily_Unspecified_Hour() 
     475        { 
     476 
     477                /* 
     478                 * Test 1 
     479                 * 
     480                 * Context : 
     481                 *  - Cron runs at January 1 1971 09:00:00 GMT 
     482                 *  - getRescheduledTime is called within the same second as the cron task 
     483                 *  - setHour is not called, no need to control the scheduled hour 
     484                 * 
     485                 * Expected : 
     486                 *  getRescheduledTime returns January 2 1971 09:00:00 GMT 
     487                 */ 
     488                $hourlySchedule = new Piwik_ScheduledTime_Daily_Test(); 
     489                $hourlySchedule->setReturnValue('getTime', $this->JANUARY_01_1971_09_00_00); 
     490                $this->assertEqual($hourlySchedule->getRescheduledTime(), $this->JANUARY_02_1971_09_00_00); 
     491 
     492                /* 
     493                 * Test 2 
     494                 * 
     495                 * Context : 
     496                 *  - Cron runs at January 1 1971 09:00:00 GMT 
     497                 *  - getRescheduledTime is called 10 minutes after the cron task to simulate a previous task 
     498                 *  - setHour is not called, no need to control the scheduled hour 
     499                 * 
     500                 * Expected : 
     501                 *  getRescheduledTime returns January 2 1971 09:10:00 GMT 
     502                 *  The task would then be executed at January 2 1971 10:00:00 which is ok because setHour has not been called 
     503                 */ 
     504                $hourlySchedule = new Piwik_ScheduledTime_Daily_Test(); 
     505                $hourlySchedule->setReturnValue('getTime', $this->JANUARY_01_1971_09_10_00); 
     506                $this->assertEqual($hourlySchedule->getRescheduledTime(), $this->JANUARY_02_1971_09_10_00); 
     507        } 
     508 
     509        /* 
     510         * Tests getRescheduledTime on Piwik_ScheduledTime_Daily with specified hour 
     511         * 
     512         */ 
     513        public function test_getRescheduledTime_Daily_Specified_Hour() 
     514        { 
     515 
     516                /* 
     517                 * Test 1 
     518                 * 
     519                 * Context : 
     520                 *  - Cron runs at January 1 1971 09:00:00 GMT 
     521                 *  - getRescheduledTime is called within the same second as the cron task 
     522                 *  - setHour is set to 9 
     523                 * 
     524                 * Expected : 
     525                 *  getRescheduledTime returns January 2 1971 09:00:00 GMT 
     526                 */ 
     527                $hourlySchedule = new Piwik_ScheduledTime_Daily_Test(); 
     528                $hourlySchedule->setHour(9); 
     529                $hourlySchedule->setReturnValue('getTime', $this->JANUARY_01_1971_09_00_00); 
     530                $this->assertEqual($hourlySchedule->getRescheduledTime(), $this->JANUARY_02_1971_09_00_00); 
     531 
     532                /* 
     533                 * Test 2 
     534                 * 
     535                 * Context : 
     536                 *  - Cron runs at January 1 1971 09:00:00 GMT 
     537                 *  - getRescheduledTime is called 10 minutes after the cron task to simulate a previous task 
     538                 *  - setHour is set to 9 
     539                 * 
     540                 * Expected : 
     541                 *  getRescheduledTime returns January 2 1971 09:00:00 GMT 
     542                 */ 
     543                $hourlySchedule = new Piwik_ScheduledTime_Daily_Test(); 
     544                $hourlySchedule->setHour(9); 
     545                $hourlySchedule->setReturnValue('getTime', $this->JANUARY_01_1971_09_10_00); 
     546                $this->assertEqual($hourlySchedule->getRescheduledTime(), $this->JANUARY_02_1971_09_00_00); 
     547 
     548                /* 
     549                 * Test 3 
     550                 * 
     551                 * Context : 
     552                 *  - Cron fails to run at January 1 1971 09:00:00 GMT, runs at January 1 1971 12:00:00 GMT 
     553                 *  - getRescheduledTime is called within the same second as the cron task 
     554                 *  - setHour is set to 9 
     555                 * 
     556                 * Expected : 
     557                 *  getRescheduledTime returns January 2 1971 09:00:00 GMT 
     558                 */ 
     559                $hourlySchedule = new Piwik_ScheduledTime_Daily_Test(); 
     560                $hourlySchedule->setHour(9); 
     561                $hourlySchedule->setReturnValue('getTime', $this->JANUARY_01_1971_12_00_00); 
     562                $this->assertEqual($hourlySchedule->getRescheduledTime(), $this->JANUARY_02_1971_09_00_00); 
     563 
     564                /* 
     565                 * Test 4 
     566                 * 
     567                 * Context : 
     568                 *  - Cron fails to run at January 1 1971 09:00:00 GMT, runs at January 1 1971 12:00:00 GMT 
     569                 *  - getRescheduledTime is called 10 minutes after the cron task to simulate a previous task 
     570                 *  - setHour is set to 9 
     571                 * 
     572                 * Expected : 
     573                 *  getRescheduledTime returns January 2 1971 09:00:00 GMT 
     574                 */ 
     575                $hourlySchedule = new Piwik_ScheduledTime_Daily_Test(); 
     576                $hourlySchedule->setHour(9); 
     577                $hourlySchedule->setReturnValue('getTime', $this->JANUARY_01_1971_12_10_00); 
     578                $this->assertEqual($hourlySchedule->getRescheduledTime(), $this->JANUARY_02_1971_09_00_00); 
     579        } 
     580         
     581        /* 
     582         * Tests getRescheduledTime on Piwik_ScheduledTime_Weekly with unspecified hour and unspecified day 
     583         * 
     584         */ 
     585        public function test_getRescheduledTime_Weekly_Unspecified_Hour_Unspecified_Day() 
     586        { 
     587 
     588                /* 
     589                 * Test 1 
     590                 * 
     591                 * Context : 
     592                 *  - Cron runs at January 1 1971 09:00:00 GMT 
     593                 *  - getRescheduledTime is called within the same second as the cron task 
     594                 *  - setHour is not called, no need to control the scheduled hour 
     595                 *  - setDay is not called, no need to control the scheduled day 
     596                 * 
     597                 * Expected : 
     598                 *  getRescheduledTime returns January 8 1971 09:00:00 GMT 
     599                 */ 
     600                $weeklySchedule = new Piwik_ScheduledTime_Weekly_Test(); 
     601                $weeklySchedule->setReturnValue('getTime', $this->JANUARY_01_1971_09_00_00); 
     602                $this->assertEqual($weeklySchedule->getRescheduledTime(), $this->JANUARY_08_1971_09_00_00); 
     603                 
     604                /* 
     605                 * Test 2 
     606                 * 
     607                 * Context : 
     608                 *  - Cron runs at January 1 1971 09:00:00 GMT 
     609                 *  - getRescheduledTime is called 10 minutes after the cron task to simulate a previous task 
     610                 *  - setHour is not called, no need to control the scheduled hour 
     611                 *  - setDay is not called, no need to control the scheduled day 
     612                 * 
     613                 * Expected : 
     614                 *  getRescheduledTime returns January 8 1971 09:10:00 GMT 
     615                 *  The task would then be executed at January 8 1971 10:00:00 which is ok because setHour has not been called 
     616                 */ 
     617                $weeklySchedule = new Piwik_ScheduledTime_Weekly_Test(); 
     618                $weeklySchedule->setReturnValue('getTime', $this->JANUARY_01_1971_09_10_00); 
     619                $this->assertEqual($weeklySchedule->getRescheduledTime(), $this->JANUARY_08_1971_09_10_00); 
     620                 
     621                /* 
     622                 * Test 3 
     623                 * 
     624                 * Context : 
     625                 *  - Cron runs at January 1 1971 09:00:00 GMT 
     626                 *  - getRescheduledTime is called 1 day and 10 minutes after the cron task to simulate a long previous task 
     627                 *  - setHour is not called, no need to control the scheduled hour 
     628                 *  - setDay is not called, no need to control the scheduled day 
     629                 * 
     630                 * Expected : 
     631                 *  getRescheduledTime returns January 9 1971 09:10:00 GMT 
     632                 *  The task would then be executed at January 9 1971 10:00:00 which is ok because setHour and setDay  
     633                 *  have not been called. 
     634                 */ 
     635                $weeklySchedule = new Piwik_ScheduledTime_Weekly_Test(); 
     636                $weeklySchedule->setReturnValue('getTime', $this->JANUARY_02_1971_09_10_00); 
     637                $this->assertEqual($weeklySchedule->getRescheduledTime(), $this->JANUARY_09_1971_09_10_00); 
     638 
     639        } 
     640         
     641        /* 
     642         * Tests getRescheduledTime on Piwik_ScheduledTime_Weekly with specified hour and unspecified day 
     643         * 
     644         */ 
     645        public function test_getRescheduledTime_Weekly_Specified_Hour_Unspecified_Day() 
     646        { 
     647 
     648                /* 
     649                 * Test 1 
     650                 * 
     651                 * Context : 
     652                 *  - Cron runs at January 1 1971 09:00:00 GMT 
     653                 *  - getRescheduledTime is called within the same second as the cron task 
     654                 *  - setHour is set to 9 
     655                 *  - setDay is not called, no need to control the scheduled day 
     656                 * 
     657                 * Expected : 
     658                 *  getRescheduledTime returns January 8 1971 09:00:00 GMT 
     659                 */ 
     660                $weeklySchedule = new Piwik_ScheduledTime_Weekly_Test(); 
     661                $weeklySchedule->setHour(9); 
     662                $weeklySchedule->setReturnValue('getTime', $this->JANUARY_01_1971_09_00_00); 
     663                $this->assertEqual($weeklySchedule->getRescheduledTime(), $this->JANUARY_08_1971_09_00_00); 
     664                 
     665                /* 
     666                 * Test 2 
     667                 * 
     668                 * Context : 
     669                 *  - Cron runs at January 1 1971 09:00:00 GMT 
     670                 *  - getRescheduledTime is called 10 minutes after the cron task 
     671                 *  - setHour is set to 9 
     672                 *  - setDay is not called, no need to control the scheduled day 
     673                 * 
     674                 * Expected : 
     675                 *  getRescheduledTime returns January 8 1971 09:00:00 GMT 
     676                 */ 
     677                $weeklySchedule = new Piwik_ScheduledTime_Weekly_Test(); 
     678                $weeklySchedule->setHour(9); 
     679                $weeklySchedule->setReturnValue('getTime', $this->JANUARY_01_1971_09_10_00); 
     680                $this->assertEqual($weeklySchedule->getRescheduledTime(), $this->JANUARY_08_1971_09_00_00); 
     681                 
     682                /* 
     683                 * Test 3 
     684                 * 
     685                 * Context : 
     686                 *  - Cron runs at January 1 1971 09:00:00 GMT 
     687                 *  - getRescheduledTime is called 1 day and 10 minutes after the cron task to simulate a long previous task 
     688                 *  - setHour is set to 9 
     689                 *  - setDay is not called, no need to control the scheduled day 
     690                 * 
     691                 * Expected : 
     692                 *  getRescheduledTime returns January 9 1971 09:00:00 GMT 
     693                 *  The task would then be executed at January 9 1971 09:00:00 which is ok because setDay  
     694                 *  has not been called. 
     695                 */ 
     696                $weeklySchedule = new Piwik_ScheduledTime_Weekly_Test(); 
     697                $weeklySchedule->setHour(9); 
     698                $weeklySchedule->setReturnValue('getTime', $this->JANUARY_02_1971_09_10_00); 
     699                $this->assertEqual($weeklySchedule->getRescheduledTime(), $this->JANUARY_09_1971_09_00_00); 
     700                 
     701                /* 
     702                 * Test 4 
     703                 * 
     704                 * Context : 
     705                 *  - Cron fails to run at January 1 1971 09:00:00 GMT, runs at January 1 1971 12:00:00 GMT 
     706                 *  - getRescheduledTime is called within the same second as the cron task 
     707                 *  - setHour is set to 9 
     708                 *  - setDay is not called, no need to control the scheduled day 
     709                 * 
     710                 * Expected : 
     711                 *  getRescheduledTime returns January 8 1971 09:00:00 GMT 
     712                 */ 
     713                $weeklySchedule = new Piwik_ScheduledTime_Weekly_Test(); 
     714                $weeklySchedule->setHour(9); 
     715                $weeklySchedule->setReturnValue('getTime', $this->JANUARY_01_1971_12_00_00); 
     716                $this->assertEqual($weeklySchedule->getRescheduledTime(), $this->JANUARY_08_1971_09_00_00); 
     717        }        
     718 
     719        /* 
     720         * Tests getRescheduledTime on Piwik_ScheduledTime_Weekly with unspecified hour and specified day 
     721         * 
     722         */ 
     723        public function test_getRescheduledTime_Weekly_Unspecified_Hour_Specified_Day() 
     724        { 
     725 
     726                /* 
     727                 * Test 1 
     728                 * 
     729                 * Context : 
     730                 *  - Cron runs at Monday January 4 1971 09:00:00 GMT 
     731                 *  - getRescheduledTime is called within the same second as the cron task 
     732                 *  - setDay is set to 1, Monday 
     733                 *  - setHour is not called, no need to control the scheduled hour 
     734                 * 
     735                 * Expected : 
     736                 *  getRescheduledTime returns Monday January 11 1971 09:00:00 GMT 
     737                 */ 
     738                $weeklySchedule = new Piwik_ScheduledTime_Weekly_Test(); 
     739                $weeklySchedule->setDay(1); 
     740                $weeklySchedule->setReturnValue('getTime', $this->JANUARY_04_1971_09_00_00); 
     741                $this->assertEqual($weeklySchedule->getRescheduledTime(), $this->JANUARY_11_1971_09_00_00); 
     742                 
     743                /* 
     744                 * Test 2 
     745                 * 
     746                 * Context : 
     747                 *  - Cron runs at Monday January 4 1971 09:00:00 GMT 
     748                 *  - getRescheduledTime is called 10 minutes after the cron task 
     749                 *  - setDay is set to 1 
     750                 *  - setHour is not called, no need to control the scheduled hour 
     751                 * 
     752                 * Expected : 
     753                 *  getRescheduledTime returns Monday January 11 1971 09:10:00 GMT 
     754                 *  The task will run at January 11 1971 10:00:00 GMT which is ok because setHour has not been called 
     755                 */ 
     756                $weeklySchedule = new Piwik_ScheduledTime_Weekly_Test(); 
     757                $weeklySchedule->setDay(1); 
     758                $weeklySchedule->setReturnValue('getTime', $this->JANUARY_04_1971_09_10_00); 
     759                $this->assertEqual($weeklySchedule->getRescheduledTime(), $this->JANUARY_11_1971_09_10_00); 
     760                 
     761                /* 
     762                 * Test 3 
     763                 * 
     764                 * Context : 
     765                 *  - Cron fails to run at Monday January 4 1971 09:00:00 GMT, cron restarted at January 5 1971 09:00:00 GMT 
     766                 *  - getRescheduledTime is called within the same second as the cron task 
     767                 *  - setDay is set to 1 
     768                 *  - setHour is not called, no need to control the scheduled hour 
     769                 * 
     770                 * Expected : 
     771                 *  getRescheduledTime returns January 11 1971 09:00:00 GMT 
     772                 */ 
     773                $weeklySchedule = new Piwik_ScheduledTime_Weekly_Test(); 
     774                $weeklySchedule->setDay(1); 
     775                $weeklySchedule->setReturnValue('getTime', $this->JANUARY_05_1971_09_00_00); 
     776                $this->assertEqual($weeklySchedule->getRescheduledTime(), $this->JANUARY_11_1971_09_00_00); 
     777 
     778        } 
     779         
     780        /* 
     781         * Tests getRescheduledTime on Piwik_ScheduledTime_Weekly with specified hour and specified day 
     782         * 
     783         */ 
     784        public function test_getRescheduledTime_Weekly_Specified_Hour_Specified_Day() 
     785        { 
     786 
     787                /* 
     788                 * Test 1 
     789                 * 
     790                 * Context : 
     791                 *  - Cron runs at Monday January 4 1971 09:00:00 GMT 
     792                 *  - getRescheduledTime is called within the same second as the cron task 
     793                 *  - setDay is set to 1, Monday 
     794                 *  - setHour is set to 9 
     795                 * 
     796                 * Expected : 
     797                 *  getRescheduledTime returns Monday January 11 1971 09:00:00 GMT 
     798                 */ 
     799                $weeklySchedule = new Piwik_ScheduledTime_Weekly_Test(); 
     800                $weeklySchedule->setDay(1); 
     801                $weeklySchedule->setHour(9); 
     802                $weeklySchedule->setReturnValue('getTime', $this->JANUARY_04_1971_09_00_00); 
     803                $this->assertEqual($weeklySchedule->getRescheduledTime(), $this->JANUARY_11_1971_09_00_00); 
     804                 
     805                /* 
     806                 * Test 2 
     807                 * 
     808                 * Context : 
     809                 *  - Cron runs at Monday January 4 1971 09:00:00 GMT 
     810                 *  - getRescheduledTime is called 10 minutes after the cron task 
     811                 *  - setDay is set to 1, Monday 
     812                 *  - setHour is set to 9 
     813                 * 
     814                 * Expected : 
     815                 *  getRescheduledTime returns Monday January 11 1971 09:00:00 GMT 
     816                 */ 
     817                $weeklySchedule = new Piwik_ScheduledTime_Weekly_Test(); 
     818                $weeklySchedule->setDay(1); 
     819                $weeklySchedule->setHour(9); 
     820                $weeklySchedule->setReturnValue('getTime', $this->JANUARY_04_1971_09_10_00); 
     821                $this->assertEqual($weeklySchedule->getRescheduledTime(), $this->JANUARY_11_1971_09_00_00); 
     822                 
     823                /* 
     824                 * Test 3 
     825                 * 
     826                 * Context : 
     827                 *  - Cron fails to run at Monday January 4 1971 09:00:00 GMT, cron restarted at January 5 1971 09:00:00 GMT 
     828                 *  - getRescheduledTime is called within the same second as the cron task 
     829                 *  - setDay is set to 1, Monday 
     830                 *  - setHour is set to 9 
     831                 * 
     832                 * Expected : 
     833                 *  getRescheduledTime returns Monday January 11 1971 09:00:00 GMT 
     834                 */ 
     835                $weeklySchedule = new Piwik_ScheduledTime_Weekly_Test(); 
     836                $weeklySchedule->setDay(1); 
     837                $weeklySchedule->setHour(9); 
     838                $weeklySchedule->setReturnValue('getTime', $this->JANUARY_05_1971_09_00_00); 
     839                $this->assertEqual($weeklySchedule->getRescheduledTime(), $this->JANUARY_11_1971_09_00_00); 
     840                 
     841                /* 
     842                 * Test 4 
     843                 * 
     844                 * Context : 
     845                 *  - Cron fails to run at Monday January 4 1971 09:00:00 GMT, cron restarted at January 5 1971 09:00:00 GMT 
     846                 *  - getRescheduledTime is called 10 minutes after the cron task 
     847                 *  - setDay is set to 1, Monday 
     848                 *  - setHour is set to 9 
     849                 * 
     850                 * Expected : 
     851                 *  getRescheduledTime returns Monday January 11 1971 09:00:00 GMT 
     852                 */ 
     853                $weeklySchedule = new Piwik_ScheduledTime_Weekly_Test(); 
     854                $weeklySchedule->setDay(1); 
     855                $weeklySchedule->setHour(9); 
     856                $weeklySchedule->setReturnValue('getTime', $this->JANUARY_05_1971_09_10_00); 
     857                $this->assertEqual($weeklySchedule->getRescheduledTime(), $this->JANUARY_11_1971_09_00_00);      
     858                 
     859                /* 
     860                 * Test 5 
     861                 * 
     862                 * Context : 
     863                 *  - Cron fails to run at Monday January 4 1971 09:00:00 GMT, cron restarted at January 5 1971 12:00:00 GMT 
     864                 *  - getRescheduledTime is called within the same second as the cron task 
     865                 *  - setDay is set to 1, Monday 
     866                 *  - setHour is set to 9 
     867                 * 
     868                 * Expected : 
     869                 *  getRescheduledTime returns Monday January 11 1971 09:00:00 GMT 
     870                 */ 
     871                $weeklySchedule = new Piwik_ScheduledTime_Weekly_Test(); 
     872                $weeklySchedule->setDay(1); 
     873                $weeklySchedule->setHour(9); 
     874                $weeklySchedule->setReturnValue('getTime', $this->JANUARY_05_1971_12_00_00); 
     875                $this->assertEqual($weeklySchedule->getRescheduledTime(), $this->JANUARY_11_1971_09_00_00);      
     876        } 
     877         
     878        /* 
     879         * Tests getRescheduledTime on Piwik_ScheduledTime_Monthly with unspecified hour, unspecified day and unspecified week 
     880         * 
     881         */ 
     882        public function test_getRescheduledTime_Monthly_Unspecified_Hour_Unspecified_Day_Unspecified_Week() 
     883        { 
     884                /* 
     885                 * Test 1 
     886                 * 
     887                 * Context : 
     888                 *  - Cron runs at January 1 1971 09:00:00 GMT 
     889                 *  - getRescheduledTime is called within the same second as the cron task 
     890                 *  - setHour is not called, no need to control the scheduled hour 
     891                 *  - setDay is not called, no need to control the scheduled day 
     892                 *  - setWeek is not called, no need to control the scheduled week 
     893                 * 
     894                 * Expected : 
     895                 *  getRescheduledTime returns February 1 1971 09:00:00 GMT 
     896                 */ 
     897                $monthlySchedule = new Piwik_ScheduledTime_Monthly_Test(); 
     898                $monthlySchedule->setReturnValue('getTime', $this->JANUARY_01_1971_09_00_00); 
     899                $this->assertEqual($monthlySchedule->getRescheduledTime(), $this->FEBRUARY_01_1971_09_00_00); 
     900                 
     901                /* 
     902                 * Test 2 
     903                 * 
     904                 * Context : 
     905                 *  - Cron runs at January 5 1971 09:00:00 GMT 
     906                 *  - getRescheduledTime is called within the same second as the cron task 
     907                 *  - setHour is not called, no need to control the scheduled hour 
     908                 *  - setDay is not called, no need to control the scheduled day 
     909                 *  - setWeek is not called, no need to control the scheduled week 
     910                 * 
     911                 * Expected : 
     912                 *  getRescheduledTime returns February 5 1971 09:00:00 GMT 
     913                 */ 
     914                $monthlySchedule = new Piwik_ScheduledTime_Monthly_Test(); 
     915                $monthlySchedule->setReturnValue('getTime', $this->JANUARY_05_1971_09_00_00); 
     916                $this->assertEqual($monthlySchedule->getRescheduledTime(), $this->FEBRUARY_05_1971_09_00_00); 
     917                 
     918                /* 
     919                 * Test 3 
     920                 * 
     921                 * Context : 
     922                 *  - Cron runs at February 5 1971 09:00:00 GMT 
     923                 *  - getRescheduledTime is called within the same second as the cron task 
     924                 *  - setHour is not called, no need to control the scheduled hour 
     925                 *  - setDay is not called, no need to control the scheduled day 
     926                 *  - setWeek is not called, no need to control the scheduled week 
     927                 * 
     928                 * Expected : 
     929                 *  getRescheduledTime returns March 5 1971 09:00:00 GMT 
     930                 */ 
     931                $monthlySchedule = new Piwik_ScheduledTime_Monthly_Test(); 
     932                $monthlySchedule->setReturnValue('getTime', $this->FEBRUARY_05_1971_09_00_00); 
     933                $this->assertEqual($monthlySchedule->getRescheduledTime(), $this->MARCH_05_1971_09_00_00); 
     934        } 
     935         
     936        /* 
     937         * Tests getRescheduledTime on Piwik_ScheduledTime_Monthly with unspecified hour, unspecified day and specified week 
     938         * 
     939         */ 
     940        public function test_getRescheduledTime_Monthly_Unspecified_Hour_Unspecified_Day_Specified_Week() 
     941        { 
     942                /* 
     943                 * Test 1 
     944                 * 
     945                 * Context : 
     946                 *  - Cron runs at January 1 1971 09:00:00 GMT 
     947                 *  - getRescheduledTime is called within the same second as the cron task 
     948                 *  - setHour is not called, no need to control the scheduled hour 
     949                 *  - setDay is not called, no need to control the scheduled day 
     950                 *  - setWeek is set to 1 
     951                 * 
     952                 * Expected : 
     953                 *  getRescheduledTime returns Monday February 1 1971 09:00:00 GMT 
     954                 */ 
     955                $monthlySchedule = new Piwik_ScheduledTime_Monthly_Test(); 
     956                $monthlySchedule->setWeek(1); 
     957                $monthlySchedule->setReturnValue('getTime', $this->JANUARY_01_1971_09_00_00); 
     958                $this->assertEqual($monthlySchedule->getRescheduledTime(), $this->FEBRUARY_01_1971_09_00_00); 
     959                 
     960                /* 
     961                 * Test 2 
     962                 * 
     963                 * Context : 
     964                 *  - Cron fails to run at January 1 1971 09:00:00 GMT, cron restarts at January 3 1971 12:00:00 GMT 
     965                 *  - getRescheduledTime is called 10 minutes after the cron task 
     966                 *  - setHour is not called, no need to control the scheduled hour 
     967                 *  - setDay is not called, no need to control the scheduled day 
     968                 *  - setWeek is set to 1 
     969                 * 
     970                 * Expected : 
     971                 *  getRescheduledTime returns February 3 1971 12:10:00 GMT 
     972                 */ 
     973                $monthlySchedule = new Piwik_ScheduledTime_Monthly_Test(); 
     974                $monthlySchedule->setWeek(1); 
     975                $monthlySchedule->setReturnValue('getTime', $this->JANUARY_03_1971_12_10_00); 
     976                $this->assertEqual($monthlySchedule->getRescheduledTime(), $this->FEBRUARY_03_1971_12_10_00); 
     977                 
     978                /* 
     979                 * Test 3 
     980                 * 
     981                 * Context : 
     982                 *  - Cron fails to run at January 1 1971 09:00:00 GMT, cron restarts at January 4 1971 09:00:00 GMT 
     983                 *  - getRescheduledTime is called within the same second as the cron task 
     984                 *  - setHour is not called, no need to control the scheduled hour 
     985                 *  - setDay is not called, no need to control the scheduled day 
     986                 *  - setWeek is set to 1 
     987                 * 
     988                 * Expected : 
     989                 *  getRescheduledTime returns Monday February 4 1971 09:00:00 GMT 
     990                 */ 
     991                $monthlySchedule = new Piwik_ScheduledTime_Monthly_Test(); 
     992                $monthlySchedule->setWeek(1); 
     993                $monthlySchedule->setReturnValue('getTime', $this->JANUARY_04_1971_09_00_00); 
     994                $this->assertEqual($monthlySchedule->getRescheduledTime(), $this->FEBRUARY_04_1971_09_00_00); 
     995                 
     996                /* 
     997                 * Test 4 
     998                 * 
     999                 * Context : 
     1000                 *  - Cron fails to run at January 1 1971 09:00:00 GMT, cron restarts at January 11 1971 09:00:00 GMT 
     1001                 *  - getRescheduledTime is called within the same second as the cron task 
     1002                 *  - setHour is not called, no need to control the scheduled hour 
     1003                 *  - setDay is not called, no need to control the scheduled day 
     1004                 *  - setWeek is set to 1 
     1005                 * 
     1006                 * Expected : 
     1007                 *  getRescheduledTime returns February 4 1971 09:00:00 GMT 
     1008                 */ 
     1009                $monthlySchedule = new Piwik_ScheduledTime_Monthly_Test(); 
     1010                $monthlySchedule->setWeek(1); 
     1011                $monthlySchedule->setReturnValue('getTime', $this->JANUARY_11_1971_09_00_00); 
     1012                $this->assertEqual($monthlySchedule->getRescheduledTime(), $this->FEBRUARY_04_1971_09_00_00); 
     1013                 
     1014                /* 
     1015                 * Test 5 
     1016                 * 
     1017                 * Context : 
     1018                 *  - Cron fails to run at January 1 1971 09:00:00 GMT, cron restarts at January 18 1971 09:00:00 GMT 
     1019                 *  - getRescheduledTime is called within the same second as the cron task 
     1020                 *  - setHour is not called, no need to control the scheduled hour 
     1021                 *  - setDay is not called, no need to control the scheduled day 
     1022                 *  - setWeek is set to 1 
     1023                 * 
     1024                 * Expected : 
     1025                 *  getRescheduledTime returns February 4 1971 09:00:00 GMT 
     1026                 */ 
     1027                $monthlySchedule = new Piwik_ScheduledTime_Monthly_Test(); 
     1028                $monthlySchedule->setWeek(1); 
     1029                $monthlySchedule->setReturnValue('getTime', $this->JANUARY_18_1971_09_00_00); 
     1030                $this->assertEqual($monthlySchedule->getRescheduledTime(), $this->FEBRUARY_04_1971_09_00_00); 
     1031                 
     1032                /* 
     1033                 * Test 6 
     1034                 * 
     1035                 * Context : 
     1036                 *  - Cron fails to run at January 1 1971 09:00:00 GMT, cron restarts at January 25 1971 09:00:00 GMT 
     1037                 *  - getRescheduledTime is called within the same second as the cron task 
     1038                 *  - setHour is not called, no need to control the scheduled hour 
     1039                 *  - setDay is not called, no need to control the scheduled day 
     1040                 *  - setWeek is set to 1 
     1041                 * 
     1042                 * Expected : 
     1043                 *  getRescheduledTime returns February 4 1971 09:00:00 GMT 
     1044                 */ 
     1045                $monthlySchedule = new Piwik_ScheduledTime_Monthly_Test(); 
     1046                $monthlySchedule->setWeek(1); 
     1047                $monthlySchedule->setReturnValue('getTime', $this->JANUARY_25_1971_09_00_00); 
     1048                $this->assertEqual($monthlySchedule->getRescheduledTime(), $this->FEBRUARY_04_1971_09_00_00); 
     1049                 
     1050                /* 
     1051                 * Test 7 
     1052                 * 
     1053                 * Context : 
     1054                 *  - Cron fails to run at January 1 1971 09:00:00 GMT, cron restarts at January 26 1971 09:00:00 GMT 
     1055                 *  - getRescheduledTime is called within the same second as the cron task 
     1056                 *  - setHour is not called, no need to control the scheduled hour 
     1057                 *  - setDay is not called, no need to control the scheduled day 
     1058                 *  - setWeek is set to 1 
     1059                 * 
     1060                 * Expected : 
     1061                 *  getRescheduledTime returns February 5 1971 09:00:00 GMT 
     1062                 */ 
     1063                $monthlySchedule = new Piwik_ScheduledTime_Monthly_Test(); 
     1064                $monthlySchedule->setWeek(1); 
     1065                $monthlySchedule->setReturnValue('getTime', $this->JANUARY_26_1971_09_00_00); 
     1066                $this->assertEqual($monthlySchedule->getRescheduledTime(), $this->FEBRUARY_05_1971_09_00_00); 
     1067                 
     1068                /* 
     1069                 * Test 8 
     1070                 * 
     1071                 * Context : 
     1072                 *  - Cron to runs at January 1 1971 09:00:00 GMT 
     1073                 *  - getRescheduledTime is called within the same second as the cron task 
     1074                 *  - setHour is not called, no need to control the scheduled hour 
     1075                 *  - setDay is not called, no need to control the scheduled day 
     1076                 *  - setWeek is set to 2 
     1077                 * 
     1078                 * Expected : 
     1079                 *  getRescheduledTime returns February 8 1971 09:00:00 GMT 
     1080                 */ 
     1081                $monthlySchedule = new Piwik_ScheduledTime_Monthly_Test(); 
     1082                $monthlySchedule->setWeek(2); 
     1083                $monthlySchedule->setReturnValue('getTime', $this->JANUARY_01_1971_09_00_00); 
     1084                $this->assertEqual($monthlySchedule->getRescheduledTime(), $this->FEBRUARY_08_1971_09_00_00); 
     1085        } 
     1086         
     1087        /* 
     1088         * Tests getRescheduledTime on Piwik_ScheduledTime_Monthly with unspecified hour, specified day and unspecified week 
     1089         * 
     1090         */ 
     1091        public function test_getRescheduledTime_Monthly_Unspecified_Hour_Specified_Day_Unspecified_Week() 
     1092        { 
     1093                /* 
     1094                 * Test 1 
     1095                 * 
     1096                 * Context : 
     1097                 *  - Cron runs at January 1 1971 09:00:00 GMT 
     1098                 *  - getRescheduledTime is called within the same second as the cron task 
     1099                 *  - setHour is not called, no need to control the scheduled hour 
     1100                 *  - setDay is set to 1 
     1101                 *  - setWeek is not called, no need to control the scheduled week 
     1102                 * 
     1103                 * Expected : 
     1104                 *  getRescheduledTime returns Monday February 1 1971 09:00:00 GMT 
     1105                 */ 
     1106                $monthlySchedule = new Piwik_ScheduledTime_Monthly_Test(); 
     1107                $monthlySchedule->setDay(1); 
     1108                $monthlySchedule->setReturnValue('getTime', $this->JANUARY_01_1971_09_00_00); 
     1109                $this->assertEqual($monthlySchedule->getRescheduledTime(), $this->FEBRUARY_01_1971_09_00_00); 
     1110                 
     1111                /* 
     1112                 * Test 2 
     1113                 * 
     1114                 * Context : 
     1115                 *  - Cron runs at Saturday January 2 1971 09:00:00 GMT 
     1116                 *  - getRescheduledTime is called within the same second as the cron task 
     1117                 *  - setHour is not called, no need to control the scheduled hour 
     1118                 *  - setDay is set to 2 
     1119                 *  - setWeek is not called, no need to control the scheduled week 
     1120                 * 
     1121                 * Expected : 
     1122                 *  getRescheduledTime returns Tuesday February 2 1971 09:00:00 GMT 
     1123                 */ 
     1124                $monthlySchedule = new Piwik_ScheduledTime_Monthly_Test(); 
     1125                $monthlySchedule->setDay(2); 
     1126                $monthlySchedule->setReturnValue('getTime', $this->JANUARY_02_1971_09_00_00); 
     1127                $this->assertEqual($monthlySchedule->getRescheduledTime(), $this->FEBRUARY_02_1971_09_00_00); 
     1128                 
     1129                /* 
     1130                 * Test 3 
     1131                 * 
     1132                 * Context : 
     1133                 *  - Cron runs at Saturday January 2 1971 09:00:00 GMT 
     1134                 *  - getRescheduledTime is called within the same second as the cron task 
     1135                 *  - setHour is not called, no need to control the scheduled hour 
     1136                 *  - setDay is set to 3 
     1137                 *  - setWeek is not called, no need to control the scheduled week 
     1138                 * 
     1139                 * Expected : 
     1140                 *  getRescheduledTime returns Wednesday February 3 1971 09:00:00 GMT 
     1141                 */ 
     1142                $monthlySchedule = new Piwik_ScheduledTime_Monthly_Test(); 
     1143                $monthlySchedule->setDay(3); 
     1144                $monthlySchedule->setReturnValue('getTime', $this->JANUARY_02_1971_09_00_00); 
     1145                $this->assertEqual($monthlySchedule->getRescheduledTime(), $this->FEBRUARY_03_1971_09_00_00); 
     1146        } 
     1147 
     1148        /* 
     1149         * Tests getRescheduledTime on Piwik_ScheduledTime_Monthly with specified hour, specified day and specified week 
     1150         * 
     1151         */ 
     1152        public function test_getRescheduledTime_Monthly_Specified_Hour_Specified_Day_Specified_Week() 
     1153        { 
     1154                /* 
     1155                 * Test 1 
     1156                 * 
     1157                 * Context : 
     1158                 *  - Cron fails to run at January 1 1971 09:00:00 GMT, runs at January 2 1971 12:10:00 GMT 
     1159                 *  - getRescheduledTime is called within the same second as the cron task 
     1160                 *  - setHour is set to 9 
     1161                 *  - setDay is set to 1 
     1162                 *  - setWeek is set to 1 
     1163                 * 
     1164                 * Expected : 
     1165                 *  getRescheduledTime returns Monday February 1 1971 09:00:00 GMT 
     1166                 */ 
     1167                $monthlySchedule = new Piwik_ScheduledTime_Monthly_Test(); 
     1168                $monthlySchedule->setHour(9); 
     1169                $monthlySchedule->setDay(1); 
     1170                $monthlySchedule->setWeek(1); 
     1171                $monthlySchedule->setReturnValue('getTime', $this->JANUARY_02_1971_12_10_00); 
     1172                $this->assertEqual($monthlySchedule->getRescheduledTime(), $this->FEBRUARY_01_1971_09_00_00); 
     1173                 
     1174                /* 
     1175                 * Test 2 
     1176                 * 
     1177                 * Context : 
     1178                 *  - Cron fails to run at January 1 1971 09:00:00 GMT, runs at January 2 1971 12:10:00 GMT 
     1179                 *  - getRescheduledTime is called within the same second as the cron task 
     1180                 *  - setHour is set to 9 
     1181                 *  - setDay is set to 2 
     1182                 *  - setWeek is set to 1 
     1183                 * 
     1184                 * Expected : 
     1185                 *  getRescheduledTime returns Tuesday February 2 1971 09:00:00 GMT 
     1186                 */ 
     1187                $monthlySchedule = new Piwik_ScheduledTime_Monthly_Test(); 
     1188                $monthlySchedule->setHour(9); 
     1189                $monthlySchedule->setDay(2); 
     1190                $monthlySchedule->setWeek(1); 
     1191                $monthlySchedule->setReturnValue('getTime', $this->JANUARY_02_1971_12_10_00); 
     1192                $this->assertEqual($monthlySchedule->getRescheduledTime(), $this->FEBRUARY_02_1971_09_00_00); 
     1193                 
     1194                /* 
     1195                 * Test 3 
     1196                 * 
     1197                 * Context : 
     1198                 *  - Cron fails to run at January 1 1971 09:00:00 GMT, runs at January 2 1971 12:10:00 GMT 
     1199                 *  - getRescheduledTime is called within the same second as the cron task 
     1200                 *  - setHour is set to 9 
     1201                 *  - setDay is set to 3 
     1202                 *  - setWeek is set to 1 
     1203                 * 
     1204                 * Expected : 
     1205                 *  getRescheduledTime returns Wednesday February 3 1971 09:00:00 GMT 
     1206                 */ 
     1207                $monthlySchedule = new Piwik_ScheduledTime_Monthly_Test(); 
     1208                $monthlySchedule->setHour(9); 
     1209                $monthlySchedule->setDay(3); 
     1210                $monthlySchedule->setWeek(1); 
     1211                $monthlySchedule->setReturnValue('getTime', $this->JANUARY_02_1971_12_10_00); 
     1212                $this->assertEqual($monthlySchedule->getRescheduledTime(), $this->FEBRUARY_03_1971_09_00_00); 
     1213                 
     1214                /* 
     1215                 * Test 4 
     1216                 * 
     1217                 * Context : 
     1218                 *  - Cron fails to run at January 1 1971 09:00:00 GMT, runs at January 2 1971 12:10:00 GMT 
     1219                 *  - getRescheduledTime is called within the same second as the cron task 
     1220                 *  - setHour is set to 9 
     1221                 *  - setDay is set to 1 
     1222                 *  - setWeek is set to 3 
     1223                 * 
     1224                 * Expected : 
     1225                 *  getRescheduledTime returns Monday February 15 1971 09:00:00 GMT 
     1226                 */ 
     1227                $monthlySchedule = new Piwik_ScheduledTime_Monthly_Test(); 
     1228                $monthlySchedule->setHour(9); 
     1229                $monthlySchedule->setDay(1); 
     1230                $monthlySchedule->setWeek(3); 
     1231                $monthlySchedule->setReturnValue('getTime', $this->JANUARY_02_1971_12_10_00); 
     1232                $this->assertEqual($monthlySchedule->getRescheduledTime(), $this->FEBRUARY_15_1971_09_00_00); 
     1233                 
     1234                /* 
     1235                 * Test 5 
     1236                 * 
     1237                 * Context : 
     1238                 *  - Cron fails to run at January 1 1971 09:00:00 GMT, runs at January 2 1971 12:10:00 GMT 
     1239                 *  - getRescheduledTime is called within the same second as the cron task 
     1240                 *  - setHour is set to 9 
     1241                 *  - setDay is set to 2 
     1242                 *  - setWeek is set to 3 
     1243                 * 
     1244                 * Expected : 
     1245                 *  getRescheduledTime returns Tuesday February 16 1971 09:00:00 GMT 
     1246                 */ 
     1247                $monthlySchedule = new Piwik_ScheduledTime_Monthly_Test(); 
     1248                $monthlySchedule->setHour(9); 
     1249                $monthlySchedule->setDay(2); 
     1250                $monthlySchedule->setWeek(3); 
     1251                $monthlySchedule->setReturnValue('getTime', $this->JANUARY_02_1971_12_10_00); 
     1252                $this->assertEqual($monthlySchedule->getRescheduledTime(), $this->FEBRUARY_16_1971_09_00_00); 
     1253                 
     1254                /* 
     1255                 * Test 6 
     1256                 * 
     1257                 * Context : 
     1258                 *  - Cron fails to run at January 1 1971 09:00:00 GMT, runs at January 2 1971 12:10:00 GMT 
     1259                 *  - getRescheduledTime is called within the same second as the cron task 
     1260                 *  - setHour is set to 9 
     1261                 *  - setDay is set to 3 
     1262                 *  - setWeek is set to 3 
     1263                 * 
     1264                 * Expected : 
     1265                 *  getRescheduledTime returns Wednesday February 17 1971 09:00:00 GMT 
     1266                 */ 
     1267                $monthlySchedule = new Piwik_ScheduledTime_Monthly_Test(); 
     1268                $monthlySchedule->setHour(9); 
     1269                $monthlySchedule->setDay(3); 
     1270                $monthlySchedule->setWeek(3); 
     1271                $monthlySchedule->setReturnValue('getTime', $this->JANUARY_02_1971_12_10_00); 
     1272                $this->assertEqual($monthlySchedule->getRescheduledTime(), $this->FEBRUARY_17_1971_09_00_00); 
     1273        } 
     1274} 
     1275 No newline at end of file 
  • plugins/CoreAdminHome/API.php

     
     1<?php 
     2/** 
     3 * Piwik - Open source web analytics 
     4 *  
     5 * @link http://piwik.org 
     6 * @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later 
     7 * @version $Id: API.php 2519 2010-07-16 19:51:11Z SteveG $ 
     8 *  
     9 * @category Piwik_Plugins 
     10 * @package Piwik_CoreAdminHome 
     11 */ 
     12 
     13/** 
     14 * 
     15 * @package Piwik_CoreAdminHome 
     16 */ 
     17class Piwik_CoreAdminHome_API  
     18{ 
     19        static private $instance = null; 
     20        static public function getInstance() 
     21        { 
     22                if (self::$instance == null) 
     23                { 
     24                        $c = __CLASS__; 
     25                        self::$instance = new $c(); 
     26                } 
     27                return self::$instance; 
     28        } 
     29 
     30        public function runScheduledTime ( ) 
     31        { 
     32                Piwik::checkUserIsSuperUser(); 
     33                Piwik_TaskScheduler::runTasks(); 
     34                return true; 
     35        } 
     36} 
  • plugins/CoreAdminHome/CoreAdminHome.php

     
    3131                return array(  
    3232                        'AssetManager.getCssFiles' => 'getCssFiles', 
    3333                        'AssetManager.getJsFiles' => 'getJsFiles', 
    34                         'AdminMenu.add' => 'addMenu' 
     34                        'AdminMenu.add' => 'addMenu', 
     35                        'TaskScheduler.getScheduledTasks' => 'getScheduledTasks', 
    3536                ); 
    3637        } 
    3738         
     39        function getScheduledTasks ( $notification ) 
     40        { 
     41                $tasks = &$notification->getNotificationObject(); 
     42                 
     43                $optimizeArchiveTableTask = new Piwik_ScheduledTask ( 'Piwik_CoreAdminHome', 
     44                                                                                                                        'optimizeArchiveTable', 
     45                                                                                                                        new Piwik_ScheduledTime_Monthly() ); 
     46                $tasks[] = $optimizeArchiveTableTask; 
     47        } 
     48         
    3849        function getCssFiles( $notification ) 
    3950        { 
    4051                $cssFiles = &$notification->getNotificationObject(); 
     
    6879                                                        Piwik::isUserIsSuperUser(), 
    6980                                                        $order = 6); 
    7081        } 
     82         
     83        function optimizeArchiveTable() 
     84        { 
     85                $tablesPiwik = Piwik::getTablesInstalled(); 
     86                $archiveTables = array_filter ($tablesPiwik, array("Piwik_CoreAdminHome", "isArchiveTable")); 
     87                Piwik_Exec("OPTIMIZE TABLE " . implode(",", $archiveTables) ); 
     88        } 
     89         
     90        private function isArchiveTable ( $tableName ) 
     91        { 
     92                return preg_match ( '/piwik_archive_/', $tableName ) > 0; 
     93        } 
    7194} 
  • misc/cron/archive.sh

     
    22 
    33# Description 
    44# This cron script will automatically run Piwik archiving every hour. 
     5# The script will also run scheduled tasks configured within piwik using  
     6# the event hook 'TaskScheduler.getScheduledTasks' 
    57 
    68# It automatically fetches the Super User token_auth  
    79# and triggers the archiving for all websites for all periods. 
     
    8082 
    8183echo "Piwik archiving finished." 
    8284 
     85echo "Starting TaskScheduler..." 
     86        CMD="$PHP_BIN -q $PIWIK_PATH -- module=API&method=CoreAdminHome.runScheduledTime&token_auth=$TOKEN_AUTH"; 
     87        $CMD 
     88echo "Piwik TaskScheduler finished." 
  • core/TaskScheduler.php

     
     1<?php 
     2/** 
     3 * Piwik - Open source web analytics 
     4 * 
     5 * @link http://piwik.org 
     6 * @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later 
     7 * @version $Id: TaskScheduler.php 
     8 * 
     9 * @category Piwik 
     10 * @package Piwik 
     11 */ 
     12 
     13/** 
     14 * Piwik_TaskScheduler is the class used to manage the execution of periodicaly planned task. 
     15 *  
     16 * It performs the following actions : 
     17 *      - Identifies tasks of Piwik 
     18 *  - Runs tasks 
     19 * 
     20 * @package Piwik 
     21 */ 
     22 
     23class Piwik_TaskScheduler 
     24{ 
     25        const GET_TASKS_EVENT = "TaskScheduler.getScheduledTasks"; 
     26        const TIMETABLE_OPTION_STRING = "TaskScheduler.timetable"; 
     27         
     28        /* 
     29         * runTasks collects tasks defined within piwik plugins, runs them if they are scheduled and reschedules 
     30         * the tasks that have been executed. 
     31         */ 
     32        static public function runTasks() 
     33        { 
     34                // Gets the array where rescheduled timetables are stored 
     35                $timetable = unserialize(Piwik_GetOption(self::TIMETABLE_OPTION_STRING)); 
     36                 
     37                // Collects tasks 
     38                Piwik_PostEvent(self::GET_TASKS_EVENT, $tasks); 
     39 
     40                // Loop through each task 
     41                foreach ($tasks as $task) 
     42                { 
     43                        $scheduledTime = $task->getScheduledTime(); 
     44                        $className = $task->getClassName(); 
     45                        $methodName = $task->getMethodName(); 
     46 
     47                        $fullyQualifiedMethodName = $className . '.' . $methodName; 
     48                         
     49                        $rescheduledTime = $timetable[$fullyQualifiedMethodName]; 
     50                         
     51                        /* 
     52                         * Task has to be executed if : 
     53                         *      - it is the first time, ie. rescheduledTime is not set 
     54                         *  - that task has already been executed and the current system time is greater than the 
     55                         *    rescheduled time. 
     56                         */ 
     57                        if ( !isset($rescheduledTime) || (isset($rescheduledTime) && time() >= $rescheduledTime) ) 
     58                        { 
     59                                // Updates the rescheduled time 
     60                                $timetable[$fullyQualifiedMethodName] = $scheduledTime->getRescheduledTime(); 
     61                                Piwik_SetOption(self::TIMETABLE_OPTION_STRING, serialize($timetable)); 
     62 
     63                                // Run the task 
     64                                call_user_func ( array($className,$methodName) ); 
     65                        } 
     66                } 
     67        } 
     68} 
     69 No newline at end of file 
  • core/ScheduledTime/Daily.php

     
     1<?php 
     2/** 
     3 * Piwik - Open source web analytics 
     4 *  
     5 * @link http://piwik.org 
     6 * @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later 
     7 * @version $Id: Daily.php 
     8 *  
     9 * @category Piwik 
     10 * @package Piwik 
     11 */ 
     12 
     13/** 
     14 * Piwik_ScheduledTime_Daily class is used to schedule tasks every day. 
     15 * 
     16 * @see Piwik_ScheduledTask 
     17 * @package Piwik 
     18 * @subpackage Piwik_ScheduledTime 
     19 */ 
     20class Piwik_ScheduledTime_Daily extends Piwik_ScheduledTime 
     21{        
     22        public function getRescheduledTime() 
     23        { 
     24                $currentTime = $this->getTime(); 
     25                 
     26                // Add one day 
     27                $rescheduledTime = mktime (     date('H', $currentTime),  
     28                                                                        date('i', $currentTime), 
     29                                                                        date('s', $currentTime), 
     30                                                                        date('n', $currentTime), 
     31                                                                        date('j', $currentTime) + 1, 
     32                                                                        date('Y', $currentTime) 
     33                                                                        ); 
     34 
     35                // Adjusts the scheduled hour 
     36                $rescheduledTime = $this->adjustHour($rescheduledTime); 
     37 
     38                return $rescheduledTime; 
     39        } 
     40         
     41        public function setDay($_day) 
     42        { 
     43                throw new Exception ("Method not supported"); 
     44        } 
     45         
     46        public function setWeek($_week) 
     47        { 
     48                throw new Exception ("Method not supported"); 
     49        } 
     50} 
  • core/ScheduledTime/Hourly.php

     
     1<?php 
     2/** 
     3 * Piwik - Open source web analytics 
     4 *  
     5 * @link http://piwik.org 
     6 * @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later 
     7 * @version $Id: Hourly.php 
     8 *  
     9 * @category Piwik 
     10 * @package Piwik 
     11 */ 
     12 
     13/** 
     14 * Piwik_ScheduledTime_Hourly class is used to schedule tasks every hour. 
     15 * 
     16 * @see Piwik_ScheduledTask 
     17 * @package Piwik 
     18 * @subpackage Piwik_ScheduledTime 
     19 */ 
     20class Piwik_ScheduledTime_Hourly extends Piwik_ScheduledTime 
     21{ 
     22        public function getRescheduledTime() 
     23        { 
     24                $currentTime = $this->getTime(); 
     25                 
     26                // Adds one hour and reset the number of minutes 
     27                $rescheduledTime = mktime (     date('H', $currentTime) + 1,  
     28                                                                        0,  
     29                                                                        date('s', $currentTime), 
     30                                                                        date('n', $currentTime), 
     31                                                                        date('j', $currentTime), 
     32                                                                        date('Y', $currentTime) 
     33                                                                        ); 
     34                return $rescheduledTime; 
     35        } 
     36 
     37        public function setHour($_hour) 
     38        { 
     39                throw new Exception ("Method not supported"); 
     40        } 
     41         
     42        public function setDay($_day) 
     43        { 
     44                throw new Exception ("Method not supported"); 
     45        } 
     46         
     47        public function setWeek($_week) 
     48        { 
     49                throw new Exception ("Method not supported"); 
     50        } 
     51         
     52} 
  • core/ScheduledTime/Monthly.php

     
     1<?php 
     2/** 
     3 * Piwik - Open source web analytics 
     4 *  
     5 * @link http://piwik.org 
     6 * @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later 
     7 * @version $Id: Monthly.php 
     8 *  
     9 * @category Piwik 
     10 * @package Piwik 
     11 */ 
     12 
     13/** 
     14 * Piwik_ScheduledTime_Monthly class is used to schedule tasks every month. 
     15 * 
     16 * @see Piwik_ScheduledTask 
     17 * @package Piwik 
     18 * @subpackage Piwik_ScheduledTime 
     19 */ 
     20class Piwik_ScheduledTime_Monthly extends Piwik_ScheduledTime 
     21{ 
     22         
     23        public function getRescheduledTime() 
     24        { 
     25                $currentTime = $this->getTime(); 
     26                 
     27                // Adds one month 
     28                $rescheduledTime = mktime (     date('H', $currentTime),  
     29                                                                        date('i', $currentTime),  
     30                                                                        date('s', $currentTime), 
     31                                                                        date('n', $currentTime) + 1, 
     32                                                                        date('j', $currentTime), 
     33                                                                        date('Y', $currentTime) 
     34                                                                        ); 
     35                                                                         
     36                // Adjusts the scheduled hour 
     37                $rescheduledTime = $this->adjustHour($rescheduledTime); 
     38                 
     39                // Adjusts the scheduled day 
     40                $rescheduledTime = $this->adjustDay($rescheduledTime); 
     41                 
     42                if ( $this->week !== null ) 
     43                { 
     44                        // Computes the week number of the scheduled month 
     45                        $rescheduledWeek = date('W', $rescheduledTime) - (4 * (date('n', $rescheduledTime)-1) ); 
     46                        $weekError = $rescheduledWeek - $this->week; 
     47                         
     48                        if ( $weekError != 0) 
     49                        {  
     50                                /* 
     51                                 * Adds or remove a multiple of 7 days to adjust the scheduled week to the one specified 
     52                                 * with setWeek() 
     53                                 */ 
     54                                $rescheduledTime = mktime (     date('H', $rescheduledTime),  
     55                                                                                        date('i', $rescheduledTime),  
     56                                                                                        date('s', $rescheduledTime), 
     57                                                                                        date('n', $rescheduledTime), 
     58                                                                                        date('j', $rescheduledTime) - (7 * $weekError), 
     59                                                                                        date('Y', $rescheduledTime) 
     60                                                                                        ); 
     61                        } 
     62                } 
     63                 
     64                return $rescheduledTime; 
     65        } 
     66} 
  • core/ScheduledTime/Weekly.php

     
     1<?php 
     2/** 
     3 * Piwik - Open source web analytics 
     4 *  
     5 * @link http://piwik.org 
     6 * @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later 
     7 * @version $Id: Weekly.php 
     8 *  
     9 * @category Piwik 
     10 * @package Piwik 
     11 */ 
     12 
     13/** 
     14 * Piwik_ScheduledTime_Weekly class is used to schedule tasks every week. 
     15 * 
     16 * @see Piwik_ScheduledTask 
     17 * @package Piwik 
     18 * @subpackage Piwik_ScheduledTime 
     19 */ 
     20class Piwik_ScheduledTime_Weekly extends Piwik_ScheduledTime 
     21{ 
     22         
     23        public function getRescheduledTime() 
     24        { 
     25                $currentTime = $this->getTime(); 
     26                 
     27                // Adds 7 days 
     28                $rescheduledTime = mktime (     date('H', $currentTime),  
     29                                                                        date('i', $currentTime), 
     30                                                                        date('s', $currentTime), 
     31                                                                        date('n', $currentTime), 
     32                                                                        date('j', $currentTime) + 7, 
     33                                                                        date('Y', $currentTime) 
     34                                                                        ); 
     35                 
     36                // Adjusts the scheduled hour 
     37                $rescheduledTime = $this->adjustHour($rescheduledTime); 
     38                 
     39                // Adjusts the scheduled day 
     40                $rescheduledTime = $this->adjustDay($rescheduledTime); 
     41                 
     42                return $rescheduledTime; 
     43        } 
     44         
     45        public function setWeek($_week) 
     46        { 
     47                throw new Exception ("Method not supported"); 
     48        } 
     49} 
  • core/ScheduledTime.php

     
     1<?php 
     2/** 
     3 * Piwik - Open source web analytics 
     4 *  
     5 * @link http://piwik.org 
     6 * @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later 
     7 * @version $Id: ScheduledTime.php 
     8 *  
     9 * @category Piwik 
     10 * @package Piwik 
     11 */ 
     12 
     13/** 
     14 * The Piwik_ScheduledTime abstract class is used as a base class for different types of scheduling intervals. 
     15 * Piwik_ScheduledTime subclasses are used to schedule tasks within Piwik. 
     16 * 
     17 * @see Piwik_ScheduledTask 
     18 * @package Piwik 
     19 * @subpackage Piwik_ScheduledTime 
     20 */ 
     21abstract class Piwik_ScheduledTime 
     22{ 
     23 
     24# 
     25        /** 
     26         * @link http://php.net/manual/en/function.date.php, format string : 'G' 
     27         * @var integer  
     28         */ 
     29        var $hour; 
     30         
     31        /** 
     32         * @link http://php.net/manual/en/function.date.php, format string : 'N' 
     33         * @var integer 
     34         */ 
     35        var $day; 
     36         
     37        /** 
     38         * Represents week number within a month (1 to 4) 
     39         * @var integer 
     40         */ 
     41        var $week; 
     42 
     43        /* 
     44         * Returns the system time used by subclasses to compute schedulings. 
     45         * This method has been introduced so unit tests can override the current system time. 
     46         */ 
     47        protected function getTime() 
     48        { 
     49                return time(); 
     50        } 
     51 
     52        /** 
     53         * Computes the next scheduled time based on the system time at which the method has been called and 
     54         * the underlying scheduling interval. 
     55         * 
     56         * @abstract 
     57         * @return integer Returns the rescheduled time measured in the number of seconds since the Unix Epoch 
     58         */ 
     59        abstract public function getRescheduledTime(); 
     60 
     61        /* 
     62         * @param  _hour the hour to set, has to be >= 0 and < 24 
     63         * @throws Exception if method not supported by subclass or parameter _hour is invalid 
     64         */ 
     65        public function setHour($_hour) 
     66        { 
     67                if (!($_hour >=0 && $_hour < 24)) 
     68                {                        
     69                        throw new Exception ("Invalid hour parameter, must be >=0 and < 24"); 
     70                } 
     71 
     72                $this->hour = $_hour; 
     73        } 
     74         
     75        /* 
     76         * @param  _day the day to set, has to be >= 1 and < 8 
     77         * @throws Exception if method not supported by subclass or parameter _hour is invalid 
     78         */ 
     79        public function setDay($_day) 
     80        { 
     81                if (!($_day >=1 && $_day < 8)) 
     82                { 
     83                        throw new Exception ("Invalid day parameter, must be >=1 and < 8"); 
     84                } 
     85 
     86                $this->day = $_day; 
     87        } 
     88         
     89        /* 
     90         * @param  _week the week to set, has to be >= 1 and < 5 
     91         * @throws Exception if method not supported by subclass or parameter _week is invalid 
     92         */ 
     93        public function setWeek($_week) 
     94        { 
     95                if (!($_week >=1 && $_week < 5)) 
     96                { 
     97                        throw new Exception ("Invalid day parameter, must be >=1 and < 5"); 
     98                } 
     99 
     100                $this->week = $_week; 
     101        }        
     102         
     103        /* 
     104         * Computes the delta in seconds needed to adjust the rescheduled time to the required hour. 
     105         *  
     106         * @param rescheduledTime The rescheduled time to be adjusted 
     107         * @return adjusted rescheduled time 
     108         */      
     109        protected function adjustHour ($rescheduledTime) 
     110        { 
     111                if ( $this->hour !== null ) 
     112                { 
     113                        // Reset the number of minutes and set the scheduled hour to the one specified with setHour() 
     114                        $rescheduledTime = mktime (     $this->hour, 
     115                                                                                0, 
     116                                                                                date('s', $rescheduledTime), 
     117                                                                                date('n', $rescheduledTime), 
     118                                                                                date('j', $rescheduledTime), 
     119                                                                                date('Y', $rescheduledTime) 
     120                                                                                ); 
     121                } 
     122                return $rescheduledTime; 
     123        } 
     124         
     125        /* 
     126         * Computes the delta in seconds needed to adjust the rescheduled time to the required day. 
     127         *  
     128         * @param rescheduledTime The rescheduled time to be adjusted 
     129         * @return adjusted rescheduled time 
     130         */      
     131        protected function adjustDay ($rescheduledTime) 
     132        { 
     133                if ( $this->day !== null ) 
     134                { 
     135                        // Removes or adds a umber of day to set the scheduled day to the one specified with setDay() 
     136                        $rescheduledTime = mktime (     date('H', $rescheduledTime),  
     137                                                                                date('i', $rescheduledTime), 
     138                                                                                date('s', $rescheduledTime), 
     139                                                                                date('n', $rescheduledTime), 
     140                                                                                date('j', $rescheduledTime) - (date('N', $rescheduledTime) - $this->day), 
     141                                                                                date('Y', $rescheduledTime) 
     142                                                                                ); 
     143                } 
     144                 
     145                return $rescheduledTime; 
     146        } 
     147} 
  • core/ScheduledTask.php

     
     1<?php 
     2/** 
     3 * Piwik - Open source web analytics 
     4 *  
     5 * @link http://piwik.org 
     6 * @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later 
     7 * @version $Id: ScheduledTask.php 
     8 *  
     9 * @category Piwik 
     10 * @package Piwik 
     11 */ 
     12 
     13/** 
     14 * Piwik_ScheduledTask is used by the task scheduler and by plugins to configure runnable tasks. 
     15 *  
     16 * @package Piwik 
     17 * @subpackage Piwik_ScheduledTask 
     18 */ 
     19class Piwik_ScheduledTask 
     20{ 
     21    /** 
     22     * Class name where the specified method is located 
     23     * @var string  
     24     */ 
     25        var $className; 
     26# 
     27    /** 
     28     * Class method to run when task is scheduled 
     29     * @var string  
     30     */ 
     31        var $methodName; 
     32         
     33    /** 
     34     * The scheduled time policy 
     35     * @var Piwik_ScheduledTime 
     36     */ 
     37        var $scheduledTime; 
     38 
     39        function __construct( $_className, $_methodName, $_scheduledTime) 
     40        { 
     41                $this->className = $_className; 
     42                $this->methodName = $_methodName; 
     43                $this->scheduledTime = $_scheduledTime; 
     44        } 
     45         
     46        /* 
     47         * Returns class name 
     48         * @return string 
     49         */ 
     50        public function getClassName() 
     51        { 
     52                return $this->className; 
     53        } 
     54 
     55        /* 
     56         * Returns method name 
     57         * @return string 
     58         */ 
     59        public function getMethodName() 
     60        { 
     61                return $this->methodName; 
     62        } 
     63 
     64        /* 
     65         * Returns scheduled time 
     66         * @return Piwik_ScheduledTime 
     67         */ 
     68        public function getScheduledTime() 
     69        { 
     70                return $this->scheduledTime; 
     71        } 
     72}