PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/cases/models/attachment.test.php

http://github.com/davidpersson/media
PHP | 301 lines | 238 code | 40 blank | 23 comment | 3 complexity | 3b1039e5849d4e74a2a741632fa8209a MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /**
  3. * Attachment Test Case File
  4. *
  5. * Copyright (c) 2007-2013 David Persson
  6. *
  7. * Distributed under the terms of the MIT License.
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * PHP version 5
  11. * CakePHP version 1.3
  12. *
  13. * @package media
  14. * @subpackage media.tests.cases.models
  15. * @copyright 2007-2013 David Persson <nperson@gmx.de>
  16. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  17. * @link http://github.com/davidpersson/media
  18. */
  19. App::import('Model', 'Media.Attachment');
  20. require_once 'models.php';
  21. require_once dirname(dirname(dirname(__FILE__))) . DS . 'fixtures' . DS . 'test_data.php';
  22. if (!defined('MEDIA')) {
  23. define('MEDIA', TMP . 'tests' . DS);
  24. } elseif (MEDIA != TMP . 'tests' . DS) {
  25. trigger_error('MEDIA constant already defined and not pointing to tests directory.', E_USER_ERROR);
  26. }
  27. require_once dirname(dirname(dirname(dirname(__FILE__)))) . DS . 'config' . DS . 'media.php';
  28. require_once 'Media/Process.php';
  29. require_once 'Media/Info.php';
  30. /**
  31. * Attachment Test Case Class
  32. *
  33. * @package media
  34. * @subpackage media.tests.cases.models
  35. */
  36. class AttachmentTestCase extends CakeTestCase {
  37. public $Data;
  38. public $Folder;
  39. protected $_backup = array();
  40. public $fixtures = array(
  41. 'plugin.media.movie', 'plugin.media.actor',
  42. 'plugin.media.attachment', 'plugin.media.pirate'
  43. );
  44. public function setUp() {
  45. $this->Data = new TestData();
  46. $this->Folder = new Folder(TMP . 'tests' . DS, true);
  47. new Folder($this->Folder->pwd() . 'transfer' . DS, true);
  48. new Folder($this->Folder->pwd() . 'static' . DS, true);
  49. new Folder($this->Folder->pwd() . 'filter' . DS, true);
  50. $this->_backup['config'] = Configure::read('Media');
  51. $this->_backup['process'] = Media_Process::config();
  52. $this->_backup['info'] = Media_Info::config();
  53. $s = array('convert' => 'image/png', 'zoomCrop' => array(100, 100));
  54. $m = array('convert' => 'image/png', 'fitCrop' => array(300, 300));
  55. $l = array('convert' => 'image/png', 'fit' => array(600, 440));
  56. Configure::write('Media.filter', array(
  57. 'audio' => compact('s', 'm'),
  58. 'document' => compact('s', 'm'),
  59. 'generic' => array(),
  60. 'image' => compact('s', 'm', 'l'),
  61. 'video' => compact('s', 'm')
  62. ));
  63. }
  64. public function tearDown() {
  65. $this->Data->flushFiles();
  66. $this->Folder->delete();
  67. ClassRegistry::flush();
  68. Media_Process::config($this->_backup['process']);
  69. Media_Info::config($this->_backup['info']);
  70. Configure::write('Media', $this->_backup['config']);
  71. }
  72. public function testHasOne() {
  73. $Model = $this->_model('hasOne');
  74. $file = $this->Data->getFile(array('image-jpg.jpg' => 'ta.jpg'));
  75. $data = array(
  76. 'Movie' => array('title' => 'Weekend', 'director' => 'Jean-Luc Godard'),
  77. 'Attachment' => array('file' => $file, 'model' => 'Movie')
  78. );
  79. $Model->create();
  80. $this->assertTrue($Model->saveAll($data, array('validate' => 'first')));
  81. $file = $Model->Attachment->transferred();
  82. $this->assertTrue(file_exists($file));
  83. $result = $Model->find('first', array('conditions' => array('title' => 'Weekend')));
  84. $expected = array(
  85. 'id' => 1,
  86. 'model' => 'Movie',
  87. 'foreign_key' => 4,
  88. 'dirname' => 'img',
  89. 'basename' => 'ta.jpg',
  90. 'checksum' => '1920c29e7fbe4d1ad2f9173ef4591133',
  91. 'group' => null,
  92. 'alternative' => null,
  93. );
  94. $this->assertEqual($result['Attachment'], $expected);
  95. $result = $Model->delete($Model->getLastInsertID());
  96. $this->assertTrue($result);
  97. $this->assertFalse(file_exists($this->Folder->pwd() . 'transfer' . DS . 'ta.jpg'));
  98. }
  99. public function testHasMany() {
  100. $Model = $this->_model('hasMany');
  101. $fileA = $this->Data->getFile(array('image-jpg.jpg' => 'ta.jpg'));
  102. $fileB = $this->Data->getFile(array('image-png.png' => 'tb.png'));
  103. $data = array(
  104. 'Movie' => array('title' => 'Weekend', 'director' => 'Jean-Luc Godard'),
  105. 'Attachment' => array(
  106. array('file' => $fileA, 'model' => 'Movie'),
  107. array('file' => $fileB, 'model' => 'Movie')
  108. ));
  109. $Model->create();
  110. $result = $Model->saveAll($data, array('validate' => 'first'));
  111. $this->assertTrue($result);
  112. $this->assertTrue(file_exists($this->Folder->pwd() . 'transfer' . DS . 'img' . DS . 'ta.jpg'));
  113. $this->assertTrue(file_exists($this->Folder->pwd() . 'transfer' . DS . 'img' . DS . 'tb.png'));
  114. $result = $Model->find('first', array('conditions' => array('title' => 'Weekend')));
  115. $expected = array(
  116. 0 => array(
  117. 'id' => 1,
  118. 'model' => 'Movie',
  119. 'foreign_key' => 4,
  120. 'dirname' => 'img',
  121. 'basename' => 'ta.jpg',
  122. 'checksum' => '1920c29e7fbe4d1ad2f9173ef4591133',
  123. 'group' => null,
  124. 'alternative' => null,
  125. ),
  126. 1 => array(
  127. 'id' => 2,
  128. 'model' => 'Movie',
  129. 'foreign_key' => 4,
  130. 'dirname' => 'img',
  131. 'basename' => 'tb.png',
  132. 'checksum' => '7f9af648b511f2c83b1744f42254983f',
  133. 'group' => null,
  134. 'alternative' => null,
  135. ));
  136. $this->assertEqual($result['Attachment'], $expected);
  137. $result = $Model->delete($Model->getLastInsertID());
  138. $this->assertTrue($result);
  139. $this->assertFalse(file_exists($this->Folder->pwd() . 'transfer' . DS . 'ta.jpg'));
  140. $this->assertFalse(file_exists($this->Folder->pwd() . 'transfer' . DS . 'tb.jpg'));
  141. }
  142. public function testHasManyWithMissingMediaAdapters() {
  143. Media_Process::config(array('image' => null));
  144. Media_Info::config(array('image' => null));
  145. $Model = $this->_model('hasMany');
  146. $file = $this->Data->getFile(array('image-jpg.jpg' => 'ta.jpg'));
  147. $data = array(
  148. 'Movie' => array('title' => 'Weekend', 'director' => 'Jean-Luc Godard'),
  149. 'Attachment' => array(
  150. array('file' => $file, 'model' => 'Movie'),
  151. ));
  152. $this->expectError();
  153. $this->expectError();
  154. $this->expectError();
  155. $this->expectError();
  156. $this->expectError();
  157. $this->expectError();
  158. $Model->create();
  159. $result = $Model->saveAll($data, array('validate' => 'first'));
  160. $this->assertTrue($result);
  161. $this->assertTrue(file_exists($this->Folder->pwd() . 'transfer' . DS . 'img' . DS . 'ta.jpg'));
  162. $result = $Model->find('first', array('conditions' => array('title' => 'Weekend')));
  163. $expected = array(
  164. 0 => array(
  165. 'id' => 1,
  166. 'model' => 'Movie',
  167. 'foreign_key' => 4,
  168. 'dirname' => 'img',
  169. 'basename' => 'ta.jpg',
  170. 'checksum' => '1920c29e7fbe4d1ad2f9173ef4591133',
  171. 'group' => null,
  172. 'alternative' => null,
  173. )
  174. );
  175. $this->assertEqual($result['Attachment'], $expected);
  176. }
  177. public function testGroupedHasMany() {
  178. $assoc = array(
  179. 'Photo' => array(
  180. 'className' => 'Media.Attachment',
  181. 'foreignKey' => 'foreign_key',
  182. 'conditions' => array('Photo.model' => 'Movie', 'Photo.group' => 'photo'),
  183. 'dependent' => true
  184. ));
  185. $Model = $this->_model('hasMany', $assoc);
  186. $fileA = $this->Data->getFile(array('image-png.png' => 'ta.png'));
  187. $fileB = $this->Data->getFile(array('image-png.png' => 'tb.png'));
  188. $data = array(
  189. 'Movie' => array('title' => 'Weekend', 'director' => 'Jean-Luc Godard'),
  190. 'Photo' => array(
  191. array('file' => $fileA, 'model' => 'Movie', 'group' => 'photo'),
  192. array('file' => $fileB, 'model' => 'Movie', 'group' => 'photo'),
  193. ));
  194. $Model->create();
  195. $result = $Model->saveAll($data, array('validate' => 'first'));
  196. $this->assertTrue($result);
  197. $this->assertTrue(file_exists($this->Folder->pwd() . 'transfer' . DS . 'img' . DS . 'ta.png'));
  198. $this->assertTrue(file_exists($this->Folder->pwd() . 'transfer' . DS . 'img' . DS . 'tb.png'));
  199. $result = $Model->find('first', array('conditions' => array('title' => 'Weekend')));
  200. $expected = array(
  201. 'Movie' => array(
  202. 'id' => 4,
  203. 'title' => 'Weekend',
  204. 'director' => 'Jean-Luc Godard',
  205. ),
  206. 'Actor' => array(),
  207. 'Photo' => array(
  208. 0 => array(
  209. 'id' => 1,
  210. 'model' => 'Movie',
  211. 'foreign_key' => 4,
  212. 'dirname' => 'img',
  213. 'basename' => 'ta.png',
  214. 'checksum' => '7f9af648b511f2c83b1744f42254983f',
  215. 'group' => 'photo',
  216. 'alternative' => null,
  217. ),
  218. 1 => array(
  219. 'id' => 2,
  220. 'model' => 'Movie',
  221. 'foreign_key' => 4,
  222. 'dirname' => 'img',
  223. 'basename' => 'tb.png',
  224. 'checksum' => '7f9af648b511f2c83b1744f42254983f',
  225. 'group' => 'photo',
  226. 'alternative' => null,
  227. )));
  228. $this->assertEqual($result, $expected);
  229. $result = $Model->delete($Model->getLastInsertID());
  230. $this->assertTrue($result);
  231. $this->assertFalse(file_exists($this->Folder->pwd() . 'transfer' . DS . 'photo' . DS . 'ta.png'));
  232. $this->assertFalse(file_exists($this->Folder->pwd() . 'transfer' . DS . 'photo' . DS . 'tb.png'));
  233. }
  234. protected function _model($assocType, $assoc = null) {
  235. $Model = ClassRegistry::init('Movie');
  236. if ($assoc === null) {
  237. $assoc = array(
  238. 'Attachment' => array(
  239. 'className' => 'Media.Attachment',
  240. 'foreignKey' => 'foreign_key',
  241. 'conditions' => array('Attachment.model' => 'Movie'),
  242. 'dependent' => true,
  243. ));
  244. }
  245. $Model->bindModel(array($assocType => $assoc), false);
  246. $assocModelName = key($assoc);
  247. $Model->{$assocModelName}->Behaviors->attach('Media.Transfer', array(
  248. 'transferDirectory' => $this->Folder->pwd() . 'transfer' . DS
  249. ));
  250. $Model->{$assocModelName}->Behaviors->attach('Media.Generator', array(
  251. 'baseDirectory' => $this->Folder->pwd() . 'transfer' . DS,
  252. 'filterDirectory' => $this->Folder->pwd() . 'filter' . DS
  253. ));
  254. $Model->{$assocModelName}->Behaviors->attach('Media.Coupler');
  255. $Model->{$assocModelName}->Behaviors->attach('Media.Polymorphic');
  256. $Model->{$assocModelName}->Behaviors->attach('Media.Meta', array(
  257. 'level' => 2
  258. ));
  259. return $Model;
  260. }
  261. }