LogableBehaviorTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. <?php
  2. App::uses('LogableBehavior', 'Tools.Model/Behavior');
  3. App::uses('Log', 'Tools.Model');
  4. class LogableBehaviorTest extends CakeTestCase {
  5. public $Log = null;
  6. public $fixtures = array('core.cake_session', 'plugin.tools.logable_log', 'plugin.tools.logable_book', 'plugin.tools.logable_user', 'plugin.tools.logable_comment', 'core.user');
  7. public function setUp() {
  8. parent::setUp();
  9. Configure::write('Config.language', 'eng');
  10. $this->LogableBook = ClassRegistry::init('LogableBook');
  11. $this->Log = ClassRegistry::init('LogableLog');
  12. //die(debug($this->Log->find('all')));
  13. $this->LogableUser = ClassRegistry::init('LogableUser');
  14. $this->LogableComment = ClassRegistry::init('LogableComment');
  15. }
  16. public function tearDown() {
  17. parent::tearDown();
  18. unset($this->LogableBook);
  19. unset($this->Log);
  20. unset($this->LogableUser);
  21. unset($this->LogableComment);
  22. }
  23. public function testFindLog() {
  24. // no params should give all log items of current model
  25. $result = $this->LogableBook->findLog(array('order' => 'LogableLog.id DESC'));
  26. $result = Set::combine($result, '/LogableLog/id', '/LogableLog/description');
  27. $expected = array(
  28. 5 => 'LogableBook "New Book" (7) added by LogableUser "Steven" (301).',
  29. 4 => 'LogableBook "Fifth Book" (6) deleted by LogableUser "Alexander" (66).',
  30. 2 => 'LogableBook "Fifth Book" (6) updated by LogableUser "Alexander" (66).',
  31. 1 => 'LogableBook "Fifth Book" (6) created by LogableUser "Alexander" (66).'
  32. );
  33. $this->assertEquals($expected, $result);
  34. // asking for user, but not model, so should just get users changes on current model
  35. $expected = array(
  36. 5 => 'LogableBook "New Book" (7) added by LogableUser "Steven" (301).'
  37. );
  38. $result = $this->LogableBook->findLog(array('user_id'=>301, 'order' => 'id DESC'));
  39. $result = Set::combine($result, '/LogableLog/id', '/LogableLog/description');
  40. $this->assertEquals($expected, $result);
  41. $expected = array(
  42. 5 => 'LogableBook "New Book" (7) added by LogableUser "Steven" (301).'
  43. );
  44. $result = $this->LogableBook->findLog(array('foreign_id' => 7, 'order' => 'id DESC'));
  45. $result = Set::combine($result, '/LogableLog/id', '/LogableLog/description');
  46. $this->assertEquals($expected, $result);
  47. $expected = array(
  48. 0 => array('LogableLog' => array('id' => 4)),
  49. 1 => array('LogableLog' => array('id' => 2)),
  50. 2 => array('LogableLog' => array('id' => 1))
  51. );
  52. $result = $this->LogableBook->findLog(array('foreign_id'=>6, 'fields' => array('id'), 'order' => 'id DESC'));
  53. $this->assertEquals($expected, $result);
  54. $expected = array(0 => array('LogableLog' => array('id' => 4)));
  55. $result = $this->LogableBook->findLog(array('action' => 'delete', 'fields' => array('id'), 'order' => 'id DESC'));
  56. $this->assertEquals($expected, $result);
  57. $expected = array(
  58. 0 => array('LogableLog' => array('id' => 5)),
  59. 1 => array('LogableLog' => array('id' => 1))
  60. );
  61. $result = $this->LogableBook->findLog(array('action' => 'add', 'fields' => array('id'), 'order' => 'id DESC'));
  62. $this->assertEquals($expected, $result);
  63. $expected = array(0 => array('LogableLog' => array('id' => 2)));
  64. $result = $this->LogableBook->findLog(array('action' => 'edit', 'fields' => array('id'), 'order' => 'id DESC'));
  65. $this->assertEquals($expected, $result);
  66. $expected = array(
  67. 0 => array('LogableLog' => array('id' => 5)),
  68. 1 => array('LogableLog' => array('id' => 1))
  69. );
  70. $result = $this->LogableBook->findLog(array(
  71. 'action' => 'add',
  72. 'fields' => array('id'),
  73. 'order' => 'id DESC'
  74. ));
  75. $this->assertEquals($expected, $result);
  76. $expected = array(
  77. 0 => array('LogableLog' => array('id' => 5)),
  78. 1 => array('LogableLog' => array('id' => 1))
  79. );
  80. $result = $this->LogableBook->findLog(array(
  81. 'action' => 'add',
  82. 'fields' => array('id'),
  83. 'order' => 'id DESC'
  84. ));
  85. $this->assertEquals($expected, $result);
  86. $expected = array(0 => array('LogableLog' => array('id' => 4)));
  87. $result = $this->LogableBook->findLog(array(
  88. 'fields' => array('id'),
  89. 'conditions' => array('user_id' < 300, 'action' => 'delete'),
  90. 'order' => 'id DESC'
  91. ));
  92. $this->assertEquals($expected, $result);
  93. }
  94. public function testFindLogMoreModels() {
  95. // all actions of user Steven
  96. $expected = array(
  97. 0 => array('LogableLog' => array('id' => 5)),
  98. 1 => array('LogableLog' => array('id' => 3))
  99. );
  100. $result = $this->LogableBook->findLog(array(
  101. 'fields' => array('id'),
  102. 'user_id' => 301,
  103. 'model' => false,
  104. 'order' => 'id DESC'
  105. ));
  106. $this->assertEquals($expected, $result);
  107. // all delete actions of user Alexander
  108. $expected = array(0 => array('LogableLog' => array('id' => 4)));
  109. $result = $this->LogableBook->findLog(array(
  110. 'fields' => array('id'),
  111. 'user_id' => 66,
  112. 'action' => 'delete',
  113. 'model' => false,
  114. 'order' => 'id DESC'
  115. ));
  116. $this->assertEquals($expected, $result);
  117. // get a differnt models logs
  118. $expected = array(0 => array('LogableLog' => array('id' => 3)));
  119. $result = $this->LogableBook->findLog(array(
  120. 'fields' => array('id'),
  121. 'order' => 'id ASC',
  122. 'model' => 'LogableUser',
  123. 'order' => 'id DESC'
  124. ));
  125. $this->assertEquals($expected, $result);
  126. }
  127. public function testFindUserActions() {
  128. $expected = array(
  129. 0 => array('LogableLog' => array('id' => 3)),
  130. 1 => array('LogableLog' => array('id' => 5))
  131. );
  132. $result = $this->LogableBook->findUserActions(301, array('fields' => 'id'));
  133. $this->assertEquals($expected, $result);
  134. $expected = array(
  135. array('LogableLog' => array('id' => 1, 'event' => 'Alexander added a logablebook(id 6)')),
  136. array('LogableLog' => array('id' => 4, 'event' => 'Alexander deleted the logablebook(id 6)')),
  137. array('LogableLog' => array('id' => 2, 'event' => 'Alexander edited title of logablebook(id 6)')),
  138. );
  139. $result = $this->LogableBook->findUserActions(66, array('events' => true));
  140. $this->assertEquals($expected, $result);
  141. $expected = array(
  142. 0 => array('LogableLog' => array('id' => 5))
  143. );
  144. $result = $this->LogableBook->findUserActions(301, array('fields' => 'id', 'model' => 'LogableBook'));
  145. $this->assertEquals($expected, $result);
  146. }
  147. public function testAddingModels() {
  148. $this->LogableBook->save(array('LogableBook'=>array('title'=>'Denver')));
  149. $result = $this->Log->find('last', array('fields' => array('id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change')));
  150. $this->assertEquals('Denver', $result['LogableLog']['title']);
  151. $this->assertEquals('add', $result['LogableLog']['action']);
  152. $this->assertEquals('title', $result['LogableLog']['change']);
  153. $this->assertEquals(7, $result['LogableLog']['foreign_id']);
  154. $result = Set::combine($result, '/LogableLog/id', '/LogableLog/description');
  155. $expected = array(
  156. 6 => 'LogableBook "Denver" (7) added by System.'
  157. );
  158. // check with user
  159. $this->assertEquals($expected, $result);
  160. $this->LogableBook->create();
  161. $this->LogableBook->setUserData(array('LogableUser'=>array('id'=>66, 'name'=>'Alexander')));
  162. $this->LogableBook->save(array('LogableBook'=>array('title'=>'New Orleans')));
  163. $this->LogableBook->clearUserData();
  164. $result = $this->Log->find('last', array('fields' => array('id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change')));
  165. $expected = array('LogableLog' => array(
  166. 'id' => 7,
  167. 'title' => 'New Orleans',
  168. 'description' => 'LogableBook "New Orleans" (8) added by LogableUser "Alexander" (66).',
  169. 'model' => 'LogableBook',
  170. 'foreign_id' => 8,
  171. 'action' => 'add',
  172. 'user_id' => 66,
  173. 'change' => 'title',
  174. )
  175. );
  176. $this->assertEquals($expected, $result);
  177. }
  178. public function testEditingModels() {
  179. $data = array('LogableBook' => array('id' => 5, 'title' => 'Forth book'));
  180. $this->LogableBook->save($data, false);
  181. $result = $this->Log->find('last', array('fields' => array('id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change')));
  182. $expected = array(
  183. 'LogableLog' => array(
  184. 'id' => 6,
  185. 'title' => 'Forth book',
  186. 'description' => 'LogableBook "Forth book" (5) updated by System.',
  187. 'model' => 'LogableBook',
  188. 'foreign_id' => 5,
  189. 'action' => 'edit',
  190. 'user_id' => 0,
  191. 'change' => 'title',
  192. ));
  193. $this->assertEquals($expected, $result);
  194. }
  195. public function testDeletingModels() {
  196. $this->LogableBook->delete(5);
  197. $result = $this->Log->find('last', array('fields' => array('id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change')));
  198. $expected = array(
  199. 'LogableLog' => array(
  200. 'id' => 6,
  201. 'title' => 'Fourth Book',
  202. 'description' => 'LogableBook "Fourth Book" (5) deleted by System.',
  203. 'model' => 'LogableBook',
  204. 'foreign_id' => 5,
  205. 'action' => 'delete',
  206. 'user_id' => 0,
  207. 'change' => '',
  208. ));
  209. $this->assertEquals($expected, $result);
  210. }
  211. public function testUserLogging() {
  212. $this->LogableUser->save(array('LogableUser'=>array('name'=>'Jonny')));
  213. $result = $this->Log->get(6, array('id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change'));
  214. $expected = array(
  215. 'LogableLog' => array(
  216. 'id' => 6,
  217. 'title' => 'Jonny',
  218. 'description' => 'LogableUser "Jonny" (302) added by System.',
  219. 'model' => 'LogableUser',
  220. 'foreign_id' => 302,
  221. 'action' => 'add',
  222. 'user_id' => 0,
  223. 'change' => 'name',
  224. )
  225. );
  226. // check with LogableUser
  227. $this->assertEquals($expected, $result);
  228. $this->LogableUser->delete(302);
  229. $result = $this->Log->get(7, array('id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change'));
  230. $expected = array(
  231. 'LogableLog' => array(
  232. 'id' => 7,
  233. 'title' => 'Jonny',
  234. 'description' => 'LogableUser "Jonny" (302) deleted by System.',
  235. 'model' => 'LogableUser',
  236. 'foreign_id' => 302,
  237. 'action' => 'delete',
  238. 'user_id' => 0,
  239. 'change' => '',
  240. )
  241. );
  242. // check with LogableUser
  243. $this->assertEquals($expected, $result);
  244. }
  245. public function testLoggingWithoutDisplayField() {
  246. $this->LogableComment->save(array('LogableComment'=>array('content'=>'You too?')));
  247. $result = $this->Log->get(6, array('id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change'));
  248. $expected = array(
  249. 'LogableLog' => array(
  250. 'id' => 6,
  251. 'title' => 'LogableComment (5)',
  252. 'description' => 'LogableComment (5) added by System.',
  253. 'model' => 'LogableComment',
  254. 'foreign_id' => 5,
  255. 'action' => 'add',
  256. 'user_id' => 0,
  257. 'change' => 'content',
  258. )
  259. );
  260. $this->assertEquals($expected, $result);
  261. }
  262. public function testConfigurationsWithoutDescription() {
  263. $description = $this->Log->schema('description');
  264. $this->Log->removeSchema('description');
  265. $this->LogableBook->create();
  266. $this->LogableBook->save(array('LogableBook'=>array('title'=>'Denver XYZ', 'weight'=>1)));
  267. $result = $this->Log->find('last', array('fields' => array('id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change')));
  268. $expected = array(
  269. 'LogableLog' => array(
  270. 'id' => (string)6,
  271. 'title' => 'Denver XYZ',
  272. //'description' => 'LogableBook "Denver" (7) added by System.',
  273. 'description' => '',
  274. 'model' => 'LogableBook',
  275. 'foreign_id' => (string)7,
  276. 'action' => 'add',
  277. 'user_id' => (string)0,
  278. 'change' => 'title, weight',
  279. )
  280. );
  281. $this->assertEquals($expected, $result);
  282. $data = array('LogableBook' => array('id' => 5, 'title' => 'Forth book'));
  283. $this->LogableBook->save($data, false);
  284. $result = $this->Log->find('last', array('fields' => array('id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change')));
  285. $expected = array(
  286. 'LogableLog' => array(
  287. 'id' => 7,
  288. 'title' => 'Forth book',
  289. //'description' => 'LogableBook "Forth book" (5) updated by System.',
  290. 'description' => '',
  291. 'model' => 'LogableBook',
  292. 'foreign_id' => 5,
  293. 'action' => 'edit',
  294. 'user_id' => 0,
  295. 'change' => 'title',
  296. ));
  297. $this->assertEquals($expected, $result);
  298. $this->LogableBook->delete(5);
  299. $result = $this->Log->find('last', array('fields' => array('id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change')));
  300. $expected = array(
  301. 'LogableLog' => array(
  302. 'id' => 8,
  303. 'title' => 'Forth book',
  304. //'description' => 'LogableBook "Forth book" (5) deleted by System.',
  305. 'description' => '',
  306. 'model' => 'LogableBook',
  307. 'foreign_id' => 5,
  308. 'action' => 'delete',
  309. 'user_id' => 0,
  310. 'change' => '',
  311. ));
  312. $this->assertEquals($expected, $result);
  313. $this->Log->setSchema('description', $description);
  314. }
  315. public function testConfigurationsWithoutModel() {
  316. $logSchema = $this->Log->schema();
  317. $this->Log->removeSchema('description');
  318. $this->Log->removeSchema('model');
  319. $this->Log->removeSchema('foreign_id');
  320. $this->LogableBook->create();
  321. $this->LogableBook->save(array('LogableBook'=>array('title'=>'Denver')));
  322. $result = $this->Log->find('last', array('fields' => array('id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change')));
  323. $expected = array(
  324. 'LogableLog' => array(
  325. 'id' => (string)6,
  326. 'title' => 'Denver',
  327. //'description' => 'LogableBook "Denver" (7) added by System.',
  328. 'description' => '',
  329. 'model' => '',
  330. 'foreign_id' => '0',
  331. 'action' => 'add',
  332. 'user_id' => (string)0,
  333. 'change' => 'title',
  334. )
  335. );
  336. $this->assertEquals($expected, $result);
  337. $this->LogableBook->delete(5);
  338. $result = $this->Log->find('last', array('fields' => array('id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change')));
  339. $expected = array(
  340. 'LogableLog' => array(
  341. 'id' => 7,
  342. 'title' => 'Fourth Book',
  343. //'description' => 'LogableBook "Forth book" (5) deleted by System.',
  344. 'description' => '',
  345. 'model' => '',
  346. 'foreign_id' => '0',
  347. 'action' => 'delete',
  348. 'user_id' => 0,
  349. 'change' => '',
  350. ));
  351. $this->assertEquals($expected, $result);
  352. $this->Log->setSchema(null, $logSchema);
  353. }
  354. public function testConfiguratiosWithoutUserId() {
  355. $this->Log->removeSchema('user_id');
  356. $this->LogableBook->create();
  357. $this->LogableBook->save(array('LogableBook'=>array('title'=>'New Orleans')));
  358. $result = $this->Log->find('last', array('fields' => array('id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change')));
  359. $expected = array('LogableLog' => array(
  360. 'id' => (string)6,
  361. 'title' => 'New Orleans',
  362. 'description' => 'LogableBook "New Orleans" (7) added by System.',
  363. 'model' => 'LogableBook',
  364. 'foreign_id' => (string)7,
  365. 'action' => 'add',
  366. 'user_id' => '0',
  367. 'change' => 'title',
  368. )
  369. );
  370. $this->assertEquals($expected, $result);
  371. $this->LogableBook->create();
  372. $this->LogableBook->setUserData(array('LogableUser'=>array('id'=>66, 'name'=>'Alexander')));
  373. $this->LogableBook->save(array('LogableBook'=>array('title'=>'New York')));
  374. $this->LogableBook->clearUserData();
  375. $result = $this->Log->find('last', array('fields' => array('id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change')));
  376. $expected = array('LogableLog' => array(
  377. 'id' => (string)7,
  378. 'title' => 'New York',
  379. 'description' => 'LogableBook "New York" (8) added by LogableUser "Alexander" (66).',
  380. 'model' => 'LogableBook',
  381. 'foreign_id' => (string)8,
  382. 'action' => 'add',
  383. 'user_id' => (string)66,
  384. 'change' => 'title',
  385. )
  386. );
  387. $this->assertEquals($expected, $result);
  388. }
  389. public function testConfiguratiosWithoutAction() {
  390. $this->Log->removeSchema('user_id');
  391. $this->LogableBook->create();
  392. $this->LogableBook->setUserData(array('LogableUser'=>array('id'=>66, 'name'=>'Alexander')));
  393. $this->LogableBook->save(array('LogableBook'=>array('title'=>'New Orleans')));
  394. $this->LogableBook->clearUserData();
  395. $result = $this->Log->find('last', array('fields' => array('id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change')));
  396. $expected = array('LogableLog' => array(
  397. 'id' => (string)6,
  398. 'title' => 'New Orleans',
  399. 'description' => 'LogableBook "New Orleans" (7) added by LogableUser "Alexander" (66).',
  400. 'model' => 'LogableBook',
  401. 'foreign_id' => (string)7,
  402. 'action' => 'add',
  403. 'user_id' => (string)66,
  404. 'change' => 'title',
  405. )
  406. );
  407. $this->assertEquals($expected, $result);
  408. $this->LogableBook->delete(5);
  409. $result = $this->Log->find('last', array('fields' => array('id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change')));
  410. $expected = array('LogableLog' => array(
  411. 'id' => (string)7,
  412. 'title' => 'Fourth Book',
  413. 'description' => 'LogableBook "Fourth Book" (5) deleted by System.',
  414. 'model' => 'LogableBook',
  415. 'foreign_id' => (string)5,
  416. 'action' => 'delete',
  417. 'user_id' => '0',
  418. 'change' => '',
  419. )
  420. );
  421. $this->assertEquals($expected, $result);
  422. }
  423. public function testConfiguratiosDefaults() {
  424. $this->Log->removeSchema('user_id');
  425. $this->Log->removeSchema('model');
  426. $this->Log->removeSchema('foreign_id');
  427. $this->Log->removeSchema('action');
  428. $this->Log->removeSchema('change');
  429. $this->LogableBook->create();
  430. $this->LogableBook->setUserData(array('LogableUser'=>array('id'=>66, 'name'=>'Alexander')));
  431. $this->LogableBook->save(array('LogableBook'=>array('title'=>'New Orleans')));
  432. $this->LogableBook->clearUserData();
  433. $result = $this->Log->find('last', array('fields' => array('id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change')));
  434. $expected = array('LogableLog' => array(
  435. 'id' => (string)6,
  436. 'title' => 'New Orleans',
  437. 'description' => 'LogableBook "New Orleans" (7) added by LogableUser "Alexander" (66).',
  438. 'model' => 'LogableBook',
  439. 'foreign_id' => '7',
  440. 'action' => 'add',
  441. 'user_id' => '66',
  442. 'change' => 'title'
  443. )
  444. );
  445. $this->assertEquals($expected, $result);
  446. $this->LogableBook->delete(5);
  447. $result = $this->Log->find('last', array('fields' => array('id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change')));
  448. $expected = array('LogableLog' => array(
  449. 'id' => (string)7,
  450. 'title' => 'Fourth Book',
  451. 'description' => 'LogableBook "Fourth Book" (5) deleted by System.',
  452. 'model' => 'LogableBook',
  453. 'foreign_id' => '5',
  454. 'action' => 'delete',
  455. 'user_id' => '0',
  456. 'change' => ''
  457. )
  458. );
  459. $this->assertEquals($expected, $result);
  460. }
  461. public function testConfigurationWithoutMost() {
  462. $this->LogableComment->Behaviors->load('Logable', array('descriptionIds'=> false, 'userModel'=>'LogableUser'));
  463. $this->LogableComment->setUserData(array('LogableUser'=>array('id'=>66, 'name'=>'Alexander')));
  464. $this->LogableComment->save(array('LogableComment'=>array('id'=>1, 'content'=>'You too?')));
  465. $result = $this->Log->get(6, array('id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change'));
  466. $expected = array(
  467. 'LogableLog' => array(
  468. 'id' => (string)6,
  469. 'title' => 'LogableComment (1)',
  470. 'model' => 'LogableComment',
  471. 'foreign_id' => (string)1,
  472. 'action' => 'edit',
  473. 'user_id' => (string)66,
  474. 'change' => 'content',
  475. 'description' => 'LogableComment updated by LogableUser "Alexander".',
  476. )
  477. );
  478. $this->assertEquals($expected, $result);
  479. }
  480. public function testIgnoreExtraFields() {
  481. $this->LogableComment->setUserData(array('LogableUser'=>array('id'=>66, 'name'=>'Alexander')));
  482. $this->LogableComment->save(array('LogableComment'=>array('id'=>1, 'content'=>'You too?', 'extra_field'=>'some data')));
  483. $result = $this->Log->get(6, array('id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change'));
  484. $expected = array(
  485. 'LogableLog' => array(
  486. 'id' => (string)6,
  487. 'title' => 'LogableComment (1)',
  488. 'description' => 'LogableComment (1) updated by LogableUser "Alexander" (66).',
  489. 'model' => 'LogableComment',
  490. 'foreign_id' => (string)1,
  491. 'action' => 'edit',
  492. 'user_id' => (string)66,
  493. 'change' => 'content',
  494. )
  495. );
  496. $this->assertEquals($expected, $result);
  497. }
  498. public function testIgnoreSetup() {
  499. $log_rows_before = $this->Log->find('count', array('conditions' => array('model' => 'LogableUser', 'foreign_id' => 301)));
  500. $this->LogableUser->save(array('id'=>301, 'counter' => 3));
  501. $log_rows_after = $this->Log->find('count', array('conditions' => array('model' => 'LogableUser', 'foreign_id' => 301)));
  502. $this->assertEquals($log_rows_after, $log_rows_before);
  503. $this->LogableUser->save(array('id'=>301, 'name' => 'Steven Segal', 'counter' => 77));
  504. $result = $this->Log->find('first', array(
  505. 'order' => 'LogableLog.id DESC',
  506. 'conditions' => array('model' => 'LogableUser', 'foreign_id' => 301)));
  507. $this->assertEquals($result['LogableLog']['change'], 'name');
  508. }
  509. }
  510. class LogableLog extends Log {
  511. public $recursive = -1;
  512. public $order = array('LogableLog.created'=>'DESC');
  513. public $belongsTo = array(
  514. 'LogableUser' => array(
  515. 'className' => 'LogableUser',
  516. 'foreignKey' => 'user_id',
  517. 'fields' => array('id', 'name'),
  518. ),
  519. );
  520. public function removeSchema($field) {
  521. if (!isset($this->_schema[$field])) {
  522. return;
  523. }
  524. unset($this->_schema[$field]);
  525. }
  526. public function setSchema($field, $settings) {
  527. if ($field === null) {
  528. $this->_schema = $settings;
  529. return;
  530. }
  531. $this->_schema[$field] = $settings;
  532. }
  533. }
  534. class LogableTestModel extends CakeTestModel {
  535. public $recursive = -1;
  536. }
  537. class LogableBook extends LogableTestModel {
  538. public $actsAs = array(
  539. 'Tools.Logable' => array('userModel'=>'LogableUser', 'logModel'=>'LogableLog'),
  540. //'Ordered' => array('foreign_key' => FALSE)
  541. );
  542. public $order = array('LogableBook.weight' => 'ASC');
  543. }
  544. class LogableUser extends LogableTestModel {
  545. public $actsAs = array('Tools.Logable' => array('userModel'=>'LogableUser', 'logModel'=>'LogableLog', 'ignore'=>array('counter')));
  546. }
  547. class LogableComment extends LogableTestModel {
  548. public $actsAs = array('Tools.Logable' => array('userModel'=>'LogableUser', 'logModel'=>'LogableLog', ));
  549. }