TYPO3  7.6
CleanerTaskTest.php
Go to the documentation of this file.
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 
19 
23 class CleanerTaskTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
24 {
28  protected $subject = null;
29 
33  protected function setUp()
34  {
35  $this->subject = $this->getMock(CleanerTask::class, array('dummy'), array(), '', false);
36  }
37 
41  public function getPeriodCanBeSet()
42  {
43  $period = 14;
44  $this->subject->setPeriod($period);
45 
46  $this->assertEquals($period, $this->subject->getPeriod());
47  }
48 
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 
63  public function taskBuildsCorrectQuery()
64  {
65  $GLOBALS['TCA']['pages']['ctrl']['delete'] = 'deleted';
66  $GLOBALS['TCA']['pages']['ctrl']['tstamp'] = 'tstamp';
67 
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 
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 }