ElggUpgradeTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. class ElggUpgradeTest extends PHPUnit_Framework_TestCase {
  3. /**
  4. * @var ElggUpgrade
  5. */
  6. protected $obj;
  7. public function setUp() {
  8. // required by \ElggEntity when setting the owner/container
  9. _elgg_services()->setValue('session', \ElggSession::getMock());
  10. $this->obj = $this->getMockBuilder('\ElggUpgrade')
  11. ->setMethods(null)
  12. ->getMock();
  13. $this->obj->_callable_egefps = array($this, 'mock_egefps');
  14. }
  15. public function mock_egefps($options) {
  16. return array();
  17. }
  18. public function mock_egefps_with_entities() {
  19. return array(new \stdClass());
  20. }
  21. public function mock_egefps_for_full_url($options) {
  22. return array(new \stdClass());
  23. }
  24. public function mock_egefps_for_path($options) {
  25. if ($options['private_setting_value'] === 'test') {
  26. return array(new \stdClass());
  27. } else {
  28. return array();
  29. }
  30. }
  31. public function testDefaultAttrs() {
  32. $this->assertSame('elgg_upgrade', $this->obj->subtype);
  33. $this->assertSame(0, $this->obj->container_guid);
  34. $this->assertSame(0, $this->obj->owner_guid);
  35. $this->assertSame(0, $this->obj->is_completed);
  36. }
  37. public function testSetPath() {
  38. $path = 'admin/upgrades';
  39. $this->obj->setPath($path);
  40. $this->assertSame(elgg_normalize_url($path), $this->obj->getURL());
  41. }
  42. /**
  43. * @expectedException InvalidArgumentException
  44. */
  45. public function testThrowsOnBadPath() {
  46. $path = false;
  47. $this->obj->setPath($path);
  48. }
  49. /**
  50. * @expectedException InvalidArgumentException
  51. */
  52. public function testThrowsOnDuplicatePath() {
  53. $this->obj->_callable_egefps = array($this, 'mock_egefps_with_entities');
  54. $path = 'admin/upgrades';
  55. $this->obj->setPath($path);
  56. }
  57. /**
  58. * @expectedException UnexpectedValueException
  59. * @expectedExceptionMessage ElggUpgrade objects must have a value for the upgrade_url property.
  60. */
  61. public function testThrowsOnSaveWithoutPath() {
  62. $this->obj->description = 'Test';
  63. $this->obj->title = 'Test';
  64. $this->obj->save();
  65. }
  66. /**
  67. * @expectedException UnexpectedValueException
  68. * @expectedExceptionMessage ElggUpgrade objects must have a value for the title property.
  69. */
  70. public function testThrowsOnSaveWithoutTitle() {
  71. $this->obj->setPath('test');
  72. $this->obj->description = 'Test';
  73. $this->obj->save();
  74. }
  75. /**
  76. * @expectedException UnexpectedValueException
  77. * @expectedExceptionMessage ElggUpgrade objects must have a value for the description property.
  78. */
  79. public function testThrowsOnSaveWithoutDesc() {
  80. $this->obj->setPath('test');
  81. $this->obj->title = 'Test';
  82. $this->obj->save();
  83. }
  84. public function testCanFindUpgradesByPath() {
  85. $this->obj->_callable_egefps = array($this, 'mock_egefps_for_path');
  86. $upgrade = $this->obj->getUpgradeFromPath('test');
  87. $this->assertTrue((bool)$upgrade);
  88. }
  89. public function testCanFindUpgradesByFullUrl() {
  90. $this->obj->_callable_egefps = array($this, 'mock_egefps_for_full_url');
  91. $this->obj->upgrade_url = elgg_normalize_url('test');
  92. $upgrade = $this->obj->getUpgradeFromPath('test');
  93. $this->assertTrue((bool)$upgrade);
  94. $this->assertSame('test', $upgrade->upgrade_url);
  95. }
  96. // can't test save without db mocking
  97. }