MapperTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @author Robin Appelman <robin@icewind.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Tests\Settings;
  24. use OC\DB\QueryBuilder\Literal;
  25. use OC\Settings\Mapper;
  26. use Test\TestCase;
  27. /**
  28. * @group DB
  29. */
  30. class MapperTest extends TestCase {
  31. const SECTION_PREFIX = 'test_section_';
  32. /** @var Mapper */
  33. private $mapper;
  34. public function setUp() {
  35. parent::setUp();
  36. $this->mapper = new Mapper(\OC::$server->getDatabaseConnection());
  37. }
  38. public function tearDown() {
  39. parent::tearDown();
  40. $db = \OC::$server->getDatabaseConnection();
  41. $builder = $db->getQueryBuilder();
  42. $builder->delete(Mapper::TABLE_ADMIN_SECTIONS)
  43. ->where($builder->expr()->like('id', new Literal(self::SECTION_PREFIX . '%')));
  44. $builder->delete(Mapper::TABLE_ADMIN_SETTINGS)
  45. ->where($builder->expr()->like('section', new Literal(self::SECTION_PREFIX . '%')));
  46. }
  47. public function testManipulateSettings() {
  48. $this->assertEquals(false, $this->mapper->has(Mapper::TABLE_ADMIN_SETTINGS, '\OC\Dummy'));
  49. $this->assertNotContains('\OC\Dummy', $this->mapper->getClasses(Mapper::TABLE_ADMIN_SETTINGS));
  50. $this->mapper->add(Mapper::TABLE_ADMIN_SETTINGS, [
  51. 'class' => '\OC\Dummy',
  52. 'section' => self::SECTION_PREFIX . '1',
  53. 'priority' => 5
  54. ]);
  55. $this->assertEquals(true, $this->mapper->has(Mapper::TABLE_ADMIN_SETTINGS, '\OC\Dummy'));
  56. $this->assertContains('\OC\Dummy', $this->mapper->getClasses(Mapper::TABLE_ADMIN_SETTINGS));
  57. $rows = $this->mapper->getAdminSettingsFromDB(self::SECTION_PREFIX . '1');
  58. $this->assertEquals([
  59. ['class' => '\OC\Dummy', 'priority' => 5]
  60. ], $rows);
  61. $this->mapper->update(Mapper::TABLE_ADMIN_SETTINGS, 'class', '\OC\Dummy', [
  62. 'section' => self::SECTION_PREFIX . '1', 'priority' => 15
  63. ]);
  64. $rows = $this->mapper->getAdminSettingsFromDB(self::SECTION_PREFIX . '1');
  65. $this->assertEquals([
  66. ['class' => '\OC\Dummy', 'priority' => 15]
  67. ], $rows);
  68. $this->mapper->update(Mapper::TABLE_ADMIN_SETTINGS, 'class', '\OC\Dummy', [
  69. 'section' => self::SECTION_PREFIX . '2', 'priority' => 15
  70. ]);
  71. $this->assertEquals([], $this->mapper->getAdminSettingsFromDB(self::SECTION_PREFIX . '1'));
  72. $rows = $this->mapper->getAdminSettingsFromDB(self::SECTION_PREFIX . '2');
  73. $this->assertEquals([
  74. ['class' => '\OC\Dummy', 'priority' => 15]
  75. ], $rows);
  76. $this->mapper->remove(Mapper::TABLE_ADMIN_SETTINGS, '\OC\Dummy');
  77. $this->assertEquals(false, $this->mapper->has(Mapper::TABLE_ADMIN_SETTINGS, '\OC\Dummy'));
  78. }
  79. public function testGetAdminSections() {
  80. $this->assertFalse($this->mapper->has(Mapper::TABLE_ADMIN_SECTIONS, '\OC\Dummy'));
  81. $this->mapper->add(Mapper::TABLE_ADMIN_SECTIONS, [
  82. 'id' => self::SECTION_PREFIX . '1',
  83. 'class' => '\OC\Dummy',
  84. 'priority' => 1,
  85. ]);
  86. $this->assertTrue($this->mapper->has(Mapper::TABLE_ADMIN_SECTIONS, '\OC\Dummy'));
  87. // until we add a setting for the section it's not returned
  88. $this->assertNotContains([
  89. 'class' => '\OC\Dummy',
  90. 'priority' => 1,
  91. ], $this->mapper->getAdminSectionsFromDB());
  92. $this->mapper->add(Mapper::TABLE_ADMIN_SETTINGS, [
  93. 'class' => '\OC\Dummy',
  94. 'section' => self::SECTION_PREFIX . '1',
  95. 'priority' => 5
  96. ]);
  97. $this->assertContains([
  98. 'class' => '\OC\Dummy',
  99. 'priority' => 1,
  100. ], $this->mapper->getAdminSectionsFromDB());
  101. $this->mapper->remove(Mapper::TABLE_ADMIN_SETTINGS, '\OC\Dummy');
  102. $this->assertNotContains([
  103. 'class' => '\OC\Dummy',
  104. 'priority' => 1,
  105. ], $this->mapper->getAdminSectionsFromDB());
  106. $this->mapper->remove(Mapper::TABLE_ADMIN_SECTIONS, '\OC\Dummy');
  107. $this->assertFalse($this->mapper->has(Mapper::TABLE_ADMIN_SECTIONS, '\OC\Dummy'));
  108. }
  109. }