@@ -651,4 +651,81 @@ public function testDispatchCleanup()
651
651
652
652
$ this ->cronQueueObserver ->execute ($ this ->observer );
653
653
}
654
+
655
+ /**
656
+ * Testing if mismatching schedules will be deleted and
657
+ * if disabled jobs will be deleted
658
+ */
659
+ public function testDispatchRemoveConfigMismatch ()
660
+ {
661
+ $ jobConfig = ['default ' => [
662
+ 'test_job1 ' => ['instance ' => 'CronJob ' , 'method ' => 'execute ' , 'schedule ' => '*/10 * * * * ' ],
663
+ 'test_job2 ' => ['instance ' => 'CronJob ' , 'method ' => 'execute ' , 'schedule ' => null ]
664
+ ]];
665
+ $ this ->config ->expects ($ this ->exactly (2 ))->method ('getJobs ' )->will ($ this ->returnValue ($ jobConfig ));
666
+ $ this ->request ->expects ($ this ->any ())->method ('getParam ' )->will ($ this ->returnValue ('default ' ));
667
+
668
+ $ this ->cache ->expects ($ this ->any ())->method ('load ' )->willReturnMap ([
669
+ // skip cleanup
670
+ [ProcessCronQueueObserver::CACHE_KEY_LAST_HISTORY_CLEANUP_AT . 'default ' , time () + 1000 ],
671
+ // do generation
672
+ [ProcessCronQueueObserver::CACHE_KEY_LAST_SCHEDULE_GENERATE_AT . 'default ' , time () - 1000 ],
673
+ ]);
674
+
675
+ $ jobs = [];
676
+ for ($ i = 0 ; $ i <20 ; $ i ++) {
677
+ $ time = date ('Y-m-d H:i:00 ' , strtotime ("+ $ i minutes " ));
678
+ $ jobs [] = [
679
+ 'age ' => $ time ,
680
+ 'delete ' => !preg_match ('#0$# ' , date ('i ' , strtotime ($ time )))
681
+ ];
682
+ }
683
+
684
+ foreach ($ jobs as $ job ) {
685
+ /** @var Schedule | Mock $schedule */
686
+ $ schedule = $ this ->getMockBuilder (Schedule::class)
687
+ ->disableOriginalConstructor ()
688
+ ->setMethods (['getJobCode ' , 'getScheduledAt ' , 'getStatus ' , 'delete ' , 'save ' , '__wakeup ' ])->getMock ();
689
+ $ schedule ->expects ($ this ->any ())->method ('getStatus ' )->will ($ this ->returnValue ('pending ' ));
690
+ $ schedule ->expects ($ this ->any ())->method ('getJobCode ' )->will ($ this ->returnValue ('test_job1 ' ));
691
+ $ schedule ->expects ($ this ->any ())->method ('getScheduledAt ' )->will ($ this ->returnValue ($ job ['age ' ]));
692
+ $ this ->collection ->addItem ($ schedule );
693
+ }
694
+
695
+ /** @var Schedule | Mock $schedule */
696
+ $ schedule = $ this ->getMockBuilder (Schedule::class)
697
+ ->disableOriginalConstructor ()
698
+ ->setMethods (['getJobCode ' , 'getScheduledAt ' , 'getStatus ' , 'delete ' , 'save ' , '__wakeup ' ])->getMock ();
699
+ $ schedule ->expects ($ this ->any ())->method ('getStatus ' )->will ($ this ->returnValue ('pending ' ));
700
+ $ schedule ->expects ($ this ->any ())->method ('getJobCode ' )->will ($ this ->returnValue ('test_job2 ' ));
701
+ $ schedule ->expects ($ this ->any ())->method ('getScheduledAt ' )->will ($ this ->returnValue (date ('Y-m-d H:i:00 ' )));
702
+ $ this ->collection ->addItem ($ schedule );
703
+
704
+ $ scheduleMock = $ this ->getMockBuilder (Schedule::class)->disableOriginalConstructor ()
705
+ ->setMethods (['save ' , 'getCollection ' , 'getResource ' ])->getMock ();
706
+ $ scheduleMock ->expects ($ this ->any ())->method ('getCollection ' )->will ($ this ->returnValue ($ this ->collection ));
707
+ $ scheduleMock ->expects ($ this ->any ())->method ('getResource ' )->will ($ this ->returnValue ($ this ->scheduleResource ));
708
+ $ this ->scheduleFactory ->expects ($ this ->any ())->method ('create ' )->will ($ this ->returnValue ($ scheduleMock ));
709
+
710
+ $ query = [
711
+ 'status=? ' => Schedule::STATUS_PENDING ,
712
+ 'job_code=? ' => 'test_job2 ' ,
713
+ ];
714
+ $ this ->connection ->expects ($ this ->at (0 ))->method ('delete ' )->with (null , $ query );
715
+
716
+ $ scheduledAtList = [];
717
+ foreach ($ jobs as $ job ) {
718
+ if ($ job ['delete ' ] === true ) {
719
+ $ scheduledAtList [] = $ job ['age ' ];
720
+ }
721
+ }
722
+ $ query = [
723
+ 'status=? ' => Schedule::STATUS_PENDING ,
724
+ 'job_code=? ' => 'test_job1 ' ,
725
+ 'scheduled_at in (?) ' => $ scheduledAtList ,
726
+ ];
727
+ $ this ->connection ->expects ($ this ->at (1 ))->method ('delete ' )->with (null , $ query );
728
+
729
+ $ this ->cronQueueObserver ->execute ($ this ->observer );
730
+ }
654
731
}
0 commit comments