2 namespace Shaarli\Updater
;
5 use Shaarli\Config\ConfigManager
;
7 require_once 'tests/updater/DummyUpdater.php';
8 require_once 'tests/utils/ReferenceLinkDB.php';
9 require_once 'inc/rain.tpl.class.php';
13 * Runs unit tests against the updater class.
15 class UpdaterTest
extends \PHPUnit\Framework\TestCase
18 * @var string Path to test datastore.
20 protected static $testDatastore = 'sandbox/datastore.php';
23 * @var string Config file path (without extension).
25 protected static $configFile = 'sandbox/config';
33 * Executed before each test.
35 public function setUp()
37 copy('tests/utils/config/configJson.json.php', self
::$configFile .'.json.php');
38 $this->conf
= new ConfigManager(self
::$configFile);
42 * Test UpdaterUtils::read_updates_file with an empty/missing file.
44 public function testReadEmptyUpdatesFile()
46 $this->assertEquals(array(), UpdaterUtils
::read_updates_file(''));
47 $updatesFile = $this->conf
->get('resource.data_dir') . '/updates.txt';
49 $this->assertEquals(array(), UpdaterUtils
::read_updates_file($updatesFile));
54 * Test read/write updates file.
56 public function testReadWriteUpdatesFile()
58 $updatesFile = $this->conf
->get('resource.data_dir') . '/updates.txt';
59 $updatesMethods = array('m1', 'm2', 'm3');
61 UpdaterUtils
::write_updates_file($updatesFile, $updatesMethods);
62 $readMethods = UpdaterUtils
::read_updates_file($updatesFile);
63 $this->assertEquals($readMethods, $updatesMethods);
66 $updatesMethods[] = 'm4';
67 UpdaterUtils
::write_updates_file($updatesFile, $updatesMethods);
68 $readMethods = UpdaterUtils
::read_updates_file($updatesFile);
69 $this->assertEquals($readMethods, $updatesMethods);
74 * Test errors in UpdaterUtils::write_updates_file(): empty updates file.
76 * @expectedException Exception
77 * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/
79 public function testWriteEmptyUpdatesFile()
81 UpdaterUtils
::write_updates_file('', array('test'));
85 * Test errors in UpdaterUtils::write_updates_file(): not writable updates file.
87 * @expectedException Exception
88 * @expectedExceptionMessageRegExp /Unable to write(.*)/
90 public function testWriteUpdatesFileNotWritable()
92 $updatesFile = $this->conf
->get('resource.data_dir') . '/updates.txt';
94 chmod($updatesFile, 0444);
96 @UpdaterUtils
::write_updates_file($updatesFile, array('test'));
97 } catch (Exception
$e) {
104 * Test the update() method, with no update to run.
105 * 1. Everything already run.
106 * 2. User is logged out.
108 public function testNoUpdates()
111 'updateMethodDummy1',
112 'updateMethodDummy2',
113 'updateMethodDummy3',
114 'updateMethodException',
116 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
117 $this->assertEquals(array(), $updater->update());
119 $updater = new DummyUpdater(array(), array(), $this->conf
, false);
120 $this->assertEquals(array(), $updater->update());
124 * Test the update() method, with all updates to run (except the failing one).
126 public function testUpdatesFirstTime()
128 $updates = array('updateMethodException',);
129 $expectedUpdates = array(
130 'updateMethodDummy1',
131 'updateMethodDummy2',
132 'updateMethodDummy3',
134 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
135 $this->assertEquals($expectedUpdates, $updater->update());
139 * Test the update() method, only one update to run.
141 public function testOneUpdate()
144 'updateMethodDummy1',
145 'updateMethodDummy3',
146 'updateMethodException',
148 $expectedUpdate = array('updateMethodDummy2');
150 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
151 $this->assertEquals($expectedUpdate, $updater->update());
155 * Test Update failed.
157 * @expectedException \Exception
159 public function testUpdateFailed()
162 'updateMethodDummy1',
163 'updateMethodDummy2',
164 'updateMethodDummy3',
167 $updater = new DummyUpdater($updates, array(), $this->conf
, true);