ElggCoreConfigTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * Test configuration for site and application (datalist)
  4. */
  5. class ElggCoreConfigTest extends \ElggCoreUnitTest {
  6. public function testSetConfigWithTooLongName() {
  7. // prevent the error message from being logged
  8. $old_log_level = _elgg_services()->logger->getLevel();
  9. _elgg_services()->logger->setLevel(\Elgg\Logger::OFF);
  10. $name = '';
  11. for ($i = 1; $i <= 256; $i++) {
  12. $name .= 'a';
  13. }
  14. $this->assertFalse(set_config($name, 'foo'));
  15. _elgg_services()->logger->setLevel($old_log_level);
  16. }
  17. public function testSetConfigWithNewName() {
  18. global $CONFIG;
  19. $name = 'foo' . rand(0, 1000);
  20. $value = 'test';
  21. $this->assertTrue(set_config($name, $value, 22));
  22. $this->assertTrue(!isset($CONFIG->$name));
  23. $this->assertEqual($value, get_config($name, 22));
  24. $this->assertTrue(unset_config($name, 22));
  25. }
  26. public function testSetConfigWithUsedName() {
  27. $name = 'foo' . rand(0, 1000);
  28. $value = 'test';
  29. $this->assertTrue(set_config($name, 'not test', 22));
  30. $this->assertTrue(set_config($name, $value, 22));
  31. $this->assertEqual($value, get_config($name, 22));
  32. $this->assertTrue(unset_config($name, 22));
  33. }
  34. public function testSetConfigWithObject() {
  35. $name = 'foo' . rand(0, 1000);
  36. $value = new \stdClass();
  37. $value->test = true;
  38. $this->assertTrue(set_config($name, $value, 22));
  39. $this->assertIdentical($value, get_config($name, 22));
  40. $this->assertTrue(unset_config($name, 22));
  41. }
  42. public function testSetConfigWithNonexistentName() {
  43. $name = 'foo' . rand(0, 1000);
  44. $this->assertIdentical(null, get_config($name, 22));
  45. }
  46. public function testSetConfigWithCurrentSite() {
  47. global $CONFIG;
  48. $name = 'foo' . rand(0, 1000);
  49. $value = 99;
  50. $this->assertTrue(set_config($name, $value));
  51. $this->assertIdentical($value, $CONFIG->$name);
  52. $this->assertIdentical($value, get_config($name, elgg_get_site_entity()->guid));
  53. $this->assertTrue(unset_config($name));
  54. }
  55. public function testGetConfigAlreadyLoadedForCurrentSite() {
  56. global $CONFIG;
  57. $CONFIG->foo_unit_test = 35;
  58. $this->assertIdentical(35, get_config('foo_unit_test'));
  59. unset($CONFIG->foo_unit_test);
  60. }
  61. public function testGetConfigAlreadyLoadedForNotCurrentSite() {
  62. global $CONFIG;
  63. $CONFIG->foo_unit_test = 35;
  64. $this->assertIdentical(null, get_config('foo_unit_test', 34));
  65. unset($CONFIG->foo_unit_test);
  66. }
  67. public function testUnsetConfigWithNonexistentName() {
  68. $this->assertTrue(unset_config('does_not_exist'));
  69. }
  70. public function testUnsetConfigOnNotCurrentSite() {
  71. global $CONFIG;
  72. $CONFIG->foo_unit_test = 35;
  73. $this->assertIdentical(true, unset_config('foo_unit_test', 99));
  74. $this->assertIdentical(35, $CONFIG->foo_unit_test);
  75. unset($CONFIG->foo_unit_test);
  76. }
  77. public function testUnsetConfigClearsGlobalForCurrentSite() {
  78. global $CONFIG;
  79. $CONFIG->foo_unit_test = 35;
  80. $this->assertIdentical(true, unset_config('foo_unit_test'));
  81. $this->assertTrue(!isset($CONFIG->foo_unit_test));
  82. }
  83. public function testDatalistSetWithTooLongName() {
  84. // prevent the error message from being logged
  85. $old_log_level = _elgg_services()->logger->getLevel();
  86. _elgg_services()->logger->setLevel(\Elgg\Logger::OFF);
  87. $name = '';
  88. for ($i = 1; $i <= 256; $i++) {
  89. $name .= 'a';
  90. }
  91. $this->assertFalse(datalist_set($name, 'foo'));
  92. _elgg_services()->logger->setLevel($old_log_level);
  93. }
  94. public function testDatalistSetNewName() {
  95. global $CONFIG;
  96. $name = 'foo' . rand(0, 1000);
  97. $value = 'test';
  98. $this->assertTrue(datalist_set($name, $value));
  99. $this->assertEqual($value, datalist_get($name));
  100. delete_data("DELETE FROM {$CONFIG->dbprefix}datalists WHERE name = '$name'");
  101. }
  102. public function testDatalistSetWithUsedName() {
  103. global $CONFIG;
  104. $name = 'foo' . rand(0, 1000);
  105. $value = 'test';
  106. $this->assertTrue(datalist_set($name, 'not test'));
  107. $this->assertTrue(datalist_set($name, $value));
  108. $this->assertEqual($value, datalist_get($name));
  109. delete_data("DELETE FROM {$CONFIG->dbprefix}datalists WHERE name = '$name'");
  110. }
  111. public function testDatalistGetNonExistentName() {
  112. $this->assertIdentical(null, datalist_get('imaginary value'));
  113. }
  114. public function testElggSaveConfigWithArrayForDatalist() {
  115. $this->assertFalse(elgg_save_config('testing', array('1'), null));
  116. }
  117. public function testElggSaveConfigForDatalist() {
  118. global $CONFIG;
  119. $name = 'foo' . rand(0, 1000);
  120. $value = 'test';
  121. $this->assertTrue(elgg_save_config($name, $value, null));
  122. $this->assertIdentical($value, datalist_get($name));
  123. $this->assertIdentical($value, $CONFIG->$name);
  124. delete_data("DELETE FROM {$CONFIG->dbprefix}datalists WHERE name = '$name'");
  125. unset($CONFIG->$name);
  126. }
  127. public function testElggSaveConfigForCurrentSiteConfig() {
  128. global $CONFIG;
  129. $name = 'foo' . rand(0, 1000);
  130. $value = 'test';
  131. $this->assertTrue(elgg_save_config($name, $value));
  132. $this->assertIdentical($value, get_config($name));
  133. $this->assertIdentical($value, $CONFIG->$name);
  134. $this->assertTrue(unset_config($name));
  135. }
  136. public function testElggSaveConfigForNonCurrentSiteConfig() {
  137. global $CONFIG;
  138. $name = 'foo' . rand(0, 1000);
  139. $value = 'test';
  140. $this->assertTrue(elgg_save_config($name, $value, 17));
  141. $this->assertIdentical($value, get_config($name, 17));
  142. $this->assertTrue(!isset($CONFIG->$name));
  143. $this->assertTrue(unset_config($name, 17));
  144. }
  145. public function testElggGetConfigNonCurrentSiteConfig() {
  146. $name = 'foo' . rand(0, 1000);
  147. $value = 'test';
  148. $this->assertTrue(elgg_save_config($name, $value, 17));
  149. $this->assertIdentical($value, elgg_get_config($name, 17));
  150. $this->assertTrue(unset_config($name, 17));
  151. }
  152. }