WidgetsServiceTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace Elgg;
  3. class WidgetsServiceTest extends \PHPUnit_Framework_TestCase {
  4. public function elgg_set_config($key, $val) {
  5. //do nothing, that's only for BC
  6. }
  7. public function testRegisterTypeParametersControl() {
  8. $service = new \Elgg\WidgetsService(array($this, 'elgg_set_config'));
  9. $this->assertFalse($service->registerType('', 'Widget name', 'Widget description'));
  10. $this->assertFalse($service->registerType(0, 'Widget name', 'Widget description'));
  11. $this->assertFalse($service->registerType(null, 'Widget name', 'Widget description'));
  12. $this->assertFalse($service->registerType(false, 'Widget name', 'Widget description'));
  13. $this->assertFalse($service->registerType('widget_type', '', 'Widget description'));
  14. $this->assertFalse($service->registerType('widget_type', 0, 'Widget description'));
  15. $this->assertFalse($service->registerType('widget_type', null, 'Widget description'));
  16. $this->assertFalse($service->registerType('widget_type', false, 'Widget description'));
  17. }
  18. /**
  19. * Tests register, exists and unregisrer
  20. */
  21. public function testCanRegisterType() {
  22. $service = new \Elgg\WidgetsService(array($this, 'elgg_set_config'));
  23. $this->assertFalse($service->validateType('widget_type'));
  24. $this->assertFalse($service->validateType('not_registered_widget'));
  25. $this->assertTrue($service->registerType('widget_type', 'Widget name1', 'Widget description1'));
  26. $this->assertTrue($service->registerType('widget_type_con', 'Widget name2', 'Widget description2', array('dashboard', 'profile')));
  27. $this->assertTrue($service->registerType('widget_type_mul', 'Widget name3', 'Widget description3', array('all'), true));
  28. $this->assertTrue($service->registerType('widget_type_con_mul', 'Widget name4', 'Widget description4', array('dashboard', 'settings'), true));
  29. //overwrite
  30. $this->assertTrue($service->registerType('widget_type_con_mul', 'Widget name5', 'Widget description5', array('dashboard', 'settings'), true));
  31. $this->assertTrue($service->validateType('widget_type'));
  32. $this->assertTrue($service->validateType('widget_type_con'));
  33. $this->assertTrue($service->validateType('widget_type_mul'));
  34. $this->assertTrue($service->validateType('widget_type_con_mul'));
  35. $this->assertFalse($service->validateType('not_registered_widget'));
  36. return $service;
  37. }
  38. /**
  39. * @depends testCanRegisterType
  40. * @param \Elgg\WidgetsService $service
  41. */
  42. public function testRegistrationParametersPreserveContext($service) {
  43. $params = array(
  44. //exact, context, expected
  45. array(false, 'all', array('widget_type', 'widget_type_mul')),
  46. array(false, 'dashboard', array('widget_type', 'widget_type_mul', 'widget_type_con', 'widget_type_con_mul')),
  47. array(false, 'profile', array('widget_type', 'widget_type_mul', 'widget_type_con')),
  48. array(false, 'settings', array('widget_type', 'widget_type_mul', 'widget_type_con_mul')),
  49. array(true, 'all', array('widget_type', 'widget_type_mul')),
  50. array(true, 'dashboard', array('widget_type_con', 'widget_type_con_mul')),
  51. array(true, 'profile', array('widget_type_con')),
  52. array(true, 'settings', array('widget_type_con_mul')),
  53. );
  54. //is returned set of handlers the same as expected
  55. foreach ($params as $case) {
  56. list($exact, $context, $expected) = $case;
  57. sort($expected);
  58. $actual = array_keys($service->getTypes($context, $exact));
  59. sort($actual);
  60. $this->assertEquals($expected, $actual);
  61. }
  62. return $service;
  63. }
  64. /**
  65. * @depends testRegistrationParametersPreserveContext
  66. * @param \Elgg\WidgetsService $service
  67. */
  68. public function testRegistrationParametersPreserveMultiple($service) {
  69. $resps = array(
  70. 'widget_type' => false,
  71. 'widget_type_con' => false,
  72. 'widget_type_mul' => true,
  73. 'widget_type_con_mul' => true,
  74. );
  75. $contexts = array('all', 'dashboard', 'profile', 'settings');
  76. foreach (array(false, true) as $exact) {
  77. foreach ($contexts as $context) {
  78. $items = $service->getTypes($context, $exact);
  79. foreach ($items as $handler => $item) {
  80. $this->assertInstanceOf('\stdClass', $item);
  81. $this->assertNotEmpty($handler);
  82. $this->assertInternalType('string', $handler);
  83. $this->assertArrayHasKey($handler, $resps);
  84. $this->assertSame($resps[$handler], $item->multiple);
  85. }
  86. }
  87. }
  88. return $service;
  89. }
  90. /**
  91. * @depends testRegistrationParametersPreserveMultiple
  92. * @param \Elgg\WidgetsService $service
  93. */
  94. public function testRegistrationParametersPreserveNameDescription($service) {
  95. $resps = array(
  96. 'widget_type' => array('Widget name1', 'Widget description1'),
  97. 'widget_type_con' => array('Widget name2', 'Widget description2'),
  98. 'widget_type_mul' => array('Widget name3', 'Widget description3'),
  99. 'widget_type_con_mul' => array('Widget name5', 'Widget description5'),
  100. );
  101. $contexts = array('all', 'dashboard', 'profile', 'settings');
  102. foreach (array(false, true) as $exact) {
  103. foreach ($contexts as $context) {
  104. $items = $service->getTypes($context, $exact);
  105. foreach ($items as $handler => $item) {
  106. $this->assertInstanceOf('\stdClass', $item);
  107. $this->assertNotEmpty($handler);
  108. $this->assertInternalType('string', $handler);
  109. $this->assertArrayHasKey($handler, $resps);
  110. list($name, $desc) = $resps[$handler];
  111. $this->assertSame($name, $item->name);
  112. $this->assertSame($desc, $item->description);
  113. }
  114. }
  115. }
  116. return $service;
  117. }
  118. /**
  119. * @depends testRegistrationParametersPreserveNameDescription
  120. * @param \Elgg\WidgetsService $service
  121. */
  122. public function testCanUnregisterType($service) {
  123. $this->assertTrue($service->unregisterType('widget_type'));
  124. $this->assertTrue($service->unregisterType('widget_type_con'));
  125. $this->assertTrue($service->unregisterType('widget_type_mul'));
  126. $this->assertTrue($service->unregisterType('widget_type_con_mul'));
  127. $this->assertFalse($service->unregisterType('widget_not_registered'));
  128. $this->assertFalse($service->validateType('widget_type'));
  129. $this->assertFalse($service->validateType('widget_type_con'));
  130. $this->assertFalse($service->validateType('widget_type_mul'));
  131. $this->assertFalse($service->validateType('not_registered_widget'));
  132. }
  133. //TODO get, view, create, canEditLayout, defaultWidgetsInit, createDefault, defaultWidgetsPermissionsOverride
  134. }