/modules/gallery/tests/Item_Rest_Helper_Test.php

https://github.com/ffchung/gallery3 · PHP · 265 lines · 222 code · 24 blank · 19 comment · 0 complexity · 5fa3b268ef38fc2e17e304e57fd85c06 MD5 · raw file

  1. <?php defined("SYSPATH") or die("No direct script access.");
  2. /**
  3. * Gallery - a web based photo album viewer and editor
  4. * Copyright (C) 2000-2011 Bharat Mediratta
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. class Item_Rest_Helper_Test extends Gallery_Unit_Test_Case {
  21. public function teardown() {
  22. identity::set_active_user(identity::admin_user());
  23. }
  24. public function resolve_test() {
  25. $album = test::random_album();
  26. $resolved = rest::resolve(rest::url("item", $album));
  27. $this->assert_equal($album->id, $resolved->id);
  28. }
  29. public function get_scope_test() {
  30. $album1 = test::random_album();
  31. $photo1 = test::random_photo($album1);
  32. $album2 = test::random_album($album1);
  33. $photo2 = test::random_photo($album2);
  34. $album1->reload();
  35. // No scope is the same as "direct"
  36. $request = new stdClass();
  37. $request->url = rest::url("item", $album1);
  38. $request->params = new stdClass();
  39. $this->assert_equal_array(
  40. array("url" => rest::url("item", $album1),
  41. "entity" => $album1->as_restful_array(),
  42. "relationships" => array(
  43. "comments" => array(
  44. "url" => rest::url("item_comments", $album1)),
  45. "tags" => array(
  46. "url" => rest::url("item_tags", $album1),
  47. "members" => array())),
  48. "members" => array(
  49. rest::url("item", $photo1),
  50. rest::url("item", $album2)),
  51. ),
  52. item_rest::get($request));
  53. $request->url = rest::url("item", $album1);
  54. $request->params->scope = "direct";
  55. $this->assert_equal_array(
  56. array("url" => rest::url("item", $album1),
  57. "entity" => $album1->as_restful_array(),
  58. "relationships" => array(
  59. "comments" => array(
  60. "url" => rest::url("item_comments", $album1)),
  61. "tags" => array(
  62. "url" => rest::url("item_tags", $album1),
  63. "members" => array())),
  64. "members" => array(
  65. rest::url("item", $photo1),
  66. rest::url("item", $album2)),
  67. ),
  68. item_rest::get($request));
  69. $request->url = rest::url("item", $album1);
  70. $request->params->scope = "all";
  71. $this->assert_equal_array(
  72. array("url" => rest::url("item", $album1),
  73. "entity" => $album1->as_restful_array(),
  74. "relationships" => array(
  75. "comments" => array(
  76. "url" => rest::url("item_comments", $album1)),
  77. "tags" => array(
  78. "url" => rest::url("item_tags", $album1),
  79. "members" => array())),
  80. "members" => array(
  81. rest::url("item", $photo1),
  82. rest::url("item", $album2),
  83. rest::url("item", $photo2)),
  84. ),
  85. item_rest::get($request));
  86. }
  87. public function get_children_like_test() {
  88. $album1 = test::random_album();
  89. $photo1 = test::random_photo($album1);
  90. $photo2 = test::random_photo_unsaved($album1);
  91. $photo2->name = "foo.jpg";
  92. $photo2->save();
  93. $album1->reload();
  94. $request = new stdClass();
  95. $request->url = rest::url("item", $album1);
  96. $request->params = new stdClass();
  97. $request->params->name = "foo";
  98. $this->assert_equal_array(
  99. array("url" => rest::url("item", $album1),
  100. "entity" => $album1->as_restful_array(),
  101. "relationships" => array(
  102. "comments" => array(
  103. "url" => rest::url("item_comments", $album1)),
  104. "tags" => array(
  105. "url" => rest::url("item_tags", $album1),
  106. "members" => array())),
  107. "members" => array(
  108. rest::url("item", $photo2)),
  109. ),
  110. item_rest::get($request));
  111. }
  112. public function get_children_type_test() {
  113. $album1 = test::random_album();
  114. $photo1 = test::random_photo($album1);
  115. $album2 = test::random_album($album1);
  116. $album1->reload();
  117. $request = new stdClass();
  118. $request->url = rest::url("item", $album1);
  119. $request->params = new stdClass();
  120. $request->params->type = "album";
  121. $this->assert_equal_array(
  122. array("url" => rest::url("item", $album1),
  123. "entity" => $album1->as_restful_array(),
  124. "relationships" => array(
  125. "comments" => array(
  126. "url" => rest::url("item_comments", $album1)),
  127. "tags" => array(
  128. "url" => rest::url("item_tags", $album1),
  129. "members" => array())),
  130. "members" => array(
  131. rest::url("item", $album2)),
  132. ),
  133. item_rest::get($request));
  134. }
  135. public function update_album_test() {
  136. $album1 = test::random_album();
  137. access::allow(identity::everybody(), "edit", $album1);
  138. $request = new stdClass();
  139. $request->url = rest::url("item", $album1);
  140. $request->params = new stdClass();
  141. $request->params->entity = new stdClass();
  142. $request->params->entity->title = "my new title";
  143. item_rest::put($request);
  144. $this->assert_equal("my new title", $album1->reload()->title);
  145. }
  146. public function update_album_illegal_value_fails_test() {
  147. $album1 = test::random_album();
  148. access::allow(identity::everybody(), "edit", $album1);
  149. $request = new stdClass();
  150. $request->url = rest::url("item", $album1);
  151. $request->params = new stdClass();
  152. $request->params->entity = new stdClass();
  153. $request->params->entity->title = "my new title";
  154. $request->params->entity->slug = "not url safe";
  155. try {
  156. item_rest::put($request);
  157. } catch (ORM_Validation_Exception $e) {
  158. $this->assert_equal(array("slug" => "not_url_safe"), $e->validation->errors());
  159. return;
  160. }
  161. $this->assert_true(false, "Shouldn't get here");
  162. }
  163. public function add_album_test() {
  164. $album1 = test::random_album();
  165. access::allow(identity::everybody(), "edit", $album1);
  166. $request = new stdClass();
  167. $request->url = rest::url("item", $album1);
  168. $request->params = new stdClass();
  169. $request->params->entity = new stdClass();
  170. $request->params->entity->type = "album";
  171. $request->params->entity->name = "my album";
  172. $request->params->entity->title = "my album";
  173. $response = item_rest::post($request);
  174. $new_album = rest::resolve($response["url"]);
  175. $this->assert_true($new_album->is_album());
  176. $this->assert_equal($album1->id, $new_album->parent_id);
  177. }
  178. public function add_album_illegal_value_fails_test() {
  179. $album1 = test::random_album();
  180. access::allow(identity::everybody(), "edit", $album1);
  181. $request = new stdClass();
  182. $request->url = rest::url("item", $album1);
  183. $request->params = new stdClass();
  184. $request->params->entity = new stdClass();
  185. $request->params->entity->type = "album";
  186. $request->params->entity->name = "my album";
  187. $request->params->entity->title = "my album";
  188. $request->params->entity->slug = "not url safe";
  189. try {
  190. item_rest::post($request);
  191. } catch (ORM_Validation_Exception $e) {
  192. $this->assert_equal(array("slug" => "not_url_safe"), $e->validation->errors());
  193. return;
  194. }
  195. $this->assert_true(false, "Shouldn't get here");
  196. }
  197. public function add_photo_test() {
  198. $album1 = test::random_album();
  199. access::allow(identity::everybody(), "edit", $album1);
  200. $request = new stdClass();
  201. $request->url = rest::url("item", $album1);
  202. $request->params = new stdClass();
  203. $request->params->entity = new stdClass();
  204. $request->params->entity->type = "photo";
  205. $request->params->entity->name = "my photo.jpg";
  206. $request->file = MODPATH . "gallery/tests/test.jpg";
  207. $response = item_rest::post($request);
  208. $new_photo = rest::resolve($response["url"]);
  209. $this->assert_true($new_photo->is_photo());
  210. $this->assert_equal($album1->id, $new_photo->parent_id);
  211. }
  212. public function delete_album_test() {
  213. $album1 = test::random_album();
  214. access::allow(identity::everybody(), "edit", $album1);
  215. $request = new stdClass();
  216. $request->url = rest::url("item", $album1);
  217. item_rest::delete($request);
  218. $album1->reload();
  219. $this->assert_false($album1->loaded());
  220. }
  221. public function delete_album_fails_without_permission_test() {
  222. $album1 = test::random_album();
  223. access::deny(identity::everybody(), "edit", $album1);
  224. identity::set_active_user(identity::guest());
  225. $request = new stdClass();
  226. $request->url = rest::url("item", $album1);
  227. try {
  228. item_rest::delete($request);
  229. } catch (Exception $e) {
  230. $this->assert_equal("@todo FORBIDDEN", $e->getMessage());
  231. return;
  232. }
  233. $this->assert_true(false, "Shouldn't get here");
  234. }
  235. }