Skip to content
This repository was archived by the owner on Aug 9, 2021. It is now read-only.

Commit 9e87d6a

Browse files
committed
test(fdroidmarket): fix deletion of apps on update
Signed-off-by: Thierry Bugier <[email protected]>
1 parent b73b049 commit 9e87d6a

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

inc/fdroidmarket.class.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,9 @@ public function updateRepository() {
130130
$fdroid = simplexml_load_string($xml);
131131
unset($xml);
132132

133+
$marketFk = $this::getForeignKeyField();
134+
$DB->query("UPDATE `" . PluginFlyvemdmFDroidApplication::getTable() . "` SET `is_available` = '0' WHERE `$marketFk`=" . $this->getID());
133135
if (isset($fdroid->application)) {
134-
$marketFk = $this::getForeignKeyField();
135-
$fdroidApplication = new PluginFlyvemdmFDroidApplication();
136-
$DB->query("UPDATE `" . PluginFlyvemdmFDroidApplication::getTable() . "` SET `is_available` = '0' WHERE `$marketFk`=" . $this->getID());
137136
foreach ($fdroid->application as $application) {
138137
$input = [
139138
'name' => Toolbox::addslashes_deep($application->name),
@@ -163,15 +162,14 @@ public function updateRepository() {
163162
}
164163
}
165164

166-
// Delete applications vanished from the repo
167-
$criteria = [$marketFk => $this->getID(), 'is_available' => '0'];
168-
if (isCommandline()) {
169-
$dbUtils = new DbUtils();
170-
$deleteCount = $dbUtils->countElementsInTable(PluginFlyvemdmFDroidApplication::getTable(), $criteria);
171-
}
172-
$fdroidApplication->deleteByCriteria($criteria);
173-
$volume = count($fdroid->application) + $deleteCount;
174165
}
166+
// Delete applications vanished from the repo
167+
$criteria = [$marketFk => $this->getID(), 'is_available' => '0'];
168+
$dbUtils = new DbUtils();
169+
$deleteCount = $dbUtils->countElementsInTable(PluginFlyvemdmFDroidApplication::getTable(), $criteria);
170+
$fdroidApplication = new PluginFlyvemdmFDroidApplication();
171+
$fdroidApplication->deleteByCriteria($criteria);
172+
$volume = count($fdroid->application) + $deleteCount;
175173

176174
return $volume;
177175
}

inc/mqttclient.class.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* ------------------------------------------------------------------------------
2424
* @author Thierry Bugier
2525
* @copyright Copyright © 2018 Teclib
26-
* @license http://www.gnu.org/licenses/agpl.txt AGPLv3+
26+
* @license AGPLv3+ http://www.gnu.org/licenses/agpl.txt
2727
* @link https://github.com/flyve-mdm/glpi-plugin
2828
* @link https://flyve-mdm.com/
2929
* ------------------------------------------------------------------------------
@@ -148,6 +148,7 @@ public function publish($topic, $message, $qos = 0, $retain = 0) {
148148
$log = new PluginFlyvemdmMqttlog();
149149
if (self::$mqtt->publish_sync($topic, $message, $qos, $retain)) {
150150
$log->saveOutgoingMqttMessage($topic, $message);
151+
151152
return true;
152153
}
153154
} else {

tests/suite-unit/PluginFlyvemdmFdroidMarket.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ public function testCronUpdateRepositories() {
146146
}
147147

148148
public function testUpdateRepository() {
149+
// Create a market instance for the test
149150
$fixtureFile = __DIR__ . '/../fixtures/fdroid-repo.xml';
150151
$this->boolean(is_readable($fixtureFile));
151152
$instance = $this->newTestedInstance();
@@ -157,10 +158,25 @@ public function testUpdateRepository() {
157158
$instance->getFromDB($instance->getID());
158159
$instance->updateRepository();
159160

161+
162+
// Check that the application found in the xml is created
160163
$fdroidApplication = new \PluginFlyvemdmFDroidApplication();
161164
$marketFk = \PluginFlyvemdmFDroidMarket::getForeignKeyField();
162165
$marketId = $instance->getID();
163166
$rows = $fdroidApplication->find("`$marketFk` = '$marketId'");
164167
$this->array($rows)->size->isEqualTo(1, json_encode($rows, JSON_PRETTY_PRINT));
168+
169+
// Emulate an update of the market with the ap removed from it
170+
$fixtureFile = __DIR__ . '/../fixtures/fdroid-repo-app-removed.xml';
171+
$instance->update([
172+
'id' => $instance->getID(),
173+
'url' => $fixtureFile,
174+
]);
175+
176+
$instance->updateRepository();
177+
178+
// Check that the app is removed from the db
179+
$rows = $fdroidApplication->find("`$marketFk` = '$marketId'");
180+
$this->array($rows)->size->isEqualTo(0, json_encode($rows, JSON_PRETTY_PRINT));
165181
}
166182
}

0 commit comments

Comments
 (0)