3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Test\src\Core\Config;
24 use Friendica\Core\Config\Capability\ISetConfigValuesTransactionally;
25 use Friendica\Core\Config\Model\DatabaseConfig;
26 use Friendica\Core\Config\Model\ReadOnlyFileConfig;
27 use Friendica\Core\Config\Model\ConfigTransaction;
28 use Friendica\Core\Config\Util\ConfigFileManager;
29 use Friendica\Core\Config\ValueObject\Cache;
30 use Friendica\Database\Database;
31 use Friendica\Test\DatabaseTest;
32 use Friendica\Test\FixtureTest;
33 use Friendica\Test\MockedTest;
34 use Friendica\Test\Util\Database\StaticDatabase;
35 use Friendica\Test\Util\VFSTrait;
36 use Mockery\Exception\InvalidCountException;
38 class ConfigTransactionTest extends FixtureTest
40 /** @var ConfigFileManager */
41 protected $configFileManager;
43 protected function setUp(): void
47 $this->configFileManager = new ConfigFileManager($this->root->url(), $this->root->url() . '/config/', $this->root->url() . '/static/');
50 public function dataTests(): array
54 'data' => include dirname(__FILE__, 4) . '/datasets/B.node.config.php',
59 public function testInstance()
61 $config = new DatabaseConfig($this->dice->create(Database::class), new Cache());
62 $configTransaction = new ConfigTransaction($config);
64 self::assertInstanceOf(ISetConfigValuesTransactionally::class, $configTransaction);
65 self::assertInstanceOf(ConfigTransaction::class, $configTransaction);
68 public function testConfigTransaction()
70 $config = new DatabaseConfig($this->dice->create(Database::class), new Cache());
71 $config->set('config', 'key1', 'value1');
72 $config->set('system', 'key2', 'value2');
73 $config->set('system', 'keyDel', 'valueDel');
74 $config->set('delete', 'keyDel', 'catDel');
76 $configTransaction = new ConfigTransaction($config);
79 $configTransaction->set('transaction', 'key3', 'value3');
80 // overwrite key-value
81 $configTransaction->set('config', 'key1', 'changedValue1');
83 $configTransaction->delete('system', 'keyDel');
84 // delete last key of category - so the category is gone
85 $configTransaction->delete('delete', 'keyDel');
87 // The main config still doesn't know about the change
88 self::assertNull($config->get('transaction', 'key3'));
89 self::assertEquals('value1', $config->get('config', 'key1'));
90 self::assertEquals('valueDel', $config->get('system', 'keyDel'));
91 self::assertEquals('catDel', $config->get('delete', 'keyDel'));
92 // The config file still doesn't know it either
95 $configTransaction->commit();
97 // Now every config and file knows the change
98 self::assertEquals('changedValue1', $config->get('config', 'key1'));
99 self::assertEquals('value3', $config->get('transaction', 'key3'));
100 self::assertNull($config->get('system', 'keyDel'));
101 self::assertNull($config->get('delete', 'keyDel'));
102 // the whole category should be gone
103 self::assertNull($tempData['delete'] ?? null);
107 * This test asserts that in empty transactions, no saveData is called, thus no config file writing was performed
109 public function testNothingToDo()
111 $this->configFileManager = \Mockery::spy(ConfigFileManager::class);
113 $config = new DatabaseConfig($this->dice->create(Database::class), new Cache());
114 $configTransaction = new ConfigTransaction($config);
116 // commit empty transaction
117 $configTransaction->commit();
120 $this->configFileManager->shouldNotHaveReceived('saveData');
121 } catch (InvalidCountException $exception) {
122 self::fail($exception);
125 // If not failed, the test ends successfully :)
126 self::assertTrue(true);