896b92a4b075555be07d70f93b41b407fb5b0609
[Packages/TYPO3.CMS.git] / typo3 / sysext / recycler / Tests / Unit / Task / CleanerTaskTest.php
1 <?php
2 namespace TYPO3\CMS\Recycler\Tests\Unit\Task;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Database\DatabaseConnection;
18 use TYPO3\CMS\Recycler\Task\CleanerTask;
19
20 /**
21 * Testcase
22 */
23 class CleanerTaskTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
24 {
25 /**
26 * @var \PHPUnit_Framework_MockObject_MockObject|CleanerTask
27 */
28 protected $subject = null;
29
30 /**
31 * sets up an instance of \TYPO3\CMS\Recycler\Task\CleanerTask
32 */
33 protected function setUp()
34 {
35 $this->subject = $this->getMock(CleanerTask::class, array('dummy'), array(), '', false);
36 }
37
38 /**
39 * @test
40 */
41 public function getPeriodCanBeSet()
42 {
43 $period = 14;
44 $this->subject->setPeriod($period);
45
46 $this->assertEquals($period, $this->subject->getPeriod());
47 }
48
49 /**
50 * @test
51 */
52 public function getTcaTablesCanBeSet()
53 {
54 $tables = array('pages', 'tt_content');
55 $this->subject->setTcaTables($tables);
56
57 $this->assertEquals($tables, $this->subject->getTcaTables());
58 }
59
60 /**
61 * @test
62 */
63 public function taskBuildsCorrectQuery()
64 {
65 $GLOBALS['TCA']['pages']['ctrl']['delete'] = 'deleted';
66 $GLOBALS['TCA']['pages']['ctrl']['tstamp'] = 'tstamp';
67
68 /** @var \PHPUnit_Framework_MockObject_MockObject|CleanerTask $subject */
69 $subject = $this->getMock(CleanerTask::class, array('getPeriodAsTimestamp'), array(), '', false);
70
71 $tables = array('pages');
72 $subject->setTcaTables($tables);
73
74 $period = 14;
75 $subject->setPeriod($period);
76 $periodAsTimestamp = strtotime('-' . $period . ' days');
77 $subject->expects($this->once())->method('getPeriodAsTimestamp')->willReturn($periodAsTimestamp);
78
79 $dbMock = $this->getMock(DatabaseConnection::class);
80 $dbMock->expects($this->once())
81 ->method('exec_DELETEquery')
82 ->with($this->equalTo('pages'), $this->equalTo('deleted = 1 AND tstamp < ' . $periodAsTimestamp));
83
84 $dbMock->expects($this->once())
85 ->method('sql_error')
86 ->will($this->returnValue(''));
87
88 $subject->setDatabaseConnection($dbMock);
89
90 $this->assertTrue($subject->execute());
91 }
92
93 /**
94 * @test
95 */
96 public function taskFailsOnError()
97 {
98 $GLOBALS['TCA']['pages']['ctrl']['delete'] = 'deleted';
99 $GLOBALS['TCA']['pages']['ctrl']['tstamp'] = 'tstamp';
100
101 $tables = array('pages');
102 $this->subject->setTcaTables($tables);
103
104 $period = 14;
105 $this->subject->setPeriod($period);
106
107 $dbMock = $this->getMock(DatabaseConnection::class);
108 $dbMock->expects($this->once())
109 ->method('sql_error')
110 ->willReturn(1049);
111
112 $this->subject->setDatabaseConnection($dbMock);
113
114 $this->assertFalse($this->subject->execute());
115 }
116 }