uuid.test 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. <?php
  2. /**
  3. * @file
  4. * Test suite for UUID module.
  5. */
  6. /**
  7. * Base class with some helper methods.
  8. */
  9. abstract class UUIDTestCase extends DrupalWebTestCase {
  10. /**
  11. * Helper function that asserts a UUID.
  12. */
  13. protected function assertUuid($uuid, $message = NULL) {
  14. $this->assertTrue(uuid_is_valid($uuid), $message);
  15. }
  16. }
  17. /**
  18. * Tests the UUID API functions.
  19. */
  20. class UUIDAPITestCase extends UUIDTestCase {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public static function getInfo() {
  25. return array(
  26. 'name' => 'UUID API',
  27. 'description' => 'Tests the UUID API functions.',
  28. 'group' => 'UUID',
  29. );
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. protected function setUp() {
  35. parent::setUp(array('uuid'));
  36. }
  37. /**
  38. * Tests uuid function calls.
  39. */
  40. public function testApiFunctions() {
  41. // This is a valid UUID, we know that.
  42. $valid_uuid = '0ab26e6b-f074-4e44-9da6-1205fa0e9761';
  43. // Test the uuid_is_valid() function.
  44. $this->assertUuid($valid_uuid, 'UUID validation works.');
  45. // The default generator is 'php'.
  46. $uuid = uuid_generate();
  47. $this->assertUuid($uuid, 'PHP generator works.');
  48. // Test the 'mysql' generator.
  49. variable_set('uuid_generator', 'mysql');
  50. drupal_static_reset('uuid_generate');
  51. $uuid = uuid_generate();
  52. $this->assertUuid($uuid, 'MySQL generator works.');
  53. }
  54. /**
  55. * Checks that schema for tables of core entities is correctly defined.
  56. */
  57. public function testSchemas() {
  58. module_load_include('install', 'uuid');
  59. $schemas = drupal_get_schema();
  60. $field_info = uuid_schema_field_definition();
  61. $key_names = array(
  62. 'base table' => 'uuid',
  63. 'revision table' => 'revision uuid',
  64. );
  65. foreach (uuid_get_core_entity_info() as $entity_info) {
  66. // Test the fields in "base" and "revision" tables.
  67. foreach ($key_names as $table_type => $key_name) {
  68. // Table or field is not defined in entity.
  69. if (!isset($entity_info[$table_type], $entity_info['entity keys'][$key_name])) {
  70. // Not all entities have a revisions table.
  71. continue;
  72. }
  73. $field_name = $entity_info['entity keys'][$key_name];
  74. $table_name = $entity_info[$table_type];
  75. if (!isset($schemas[$table_name])) {
  76. $this->fail(sprintf('Database schema does not have a "%s" table.', $table_name));
  77. continue;
  78. }
  79. $properties = array(
  80. 'field' => array('fields', $field_info),
  81. 'index' => array('indexes', array($field_name)),
  82. );
  83. // Check integrity of the field and index definition.
  84. foreach ($properties as $type => $data) {
  85. list($property, $value) = $data;
  86. $message = sprintf('Definition of the "%s" %s in the "%s" schema', $field_name, $type, $table_name);
  87. if (isset($schemas[$table_name][$property][$field_name])) {
  88. $this->assertIdentical($schemas[$table_name][$property][$field_name], $value, "$message is correct.");
  89. }
  90. else {
  91. $this->fail("$message does not exist.");
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }
  98. /**
  99. * Tests the UUID API functions.
  100. */
  101. class UUIDV5TestCase extends UUIDTestCase {
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public static function getInfo() {
  106. return array(
  107. 'name' => 'UUID v5',
  108. 'description' => 'Tests the UUID v5 function.',
  109. 'group' => 'UUID',
  110. );
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. protected function setUp() {
  116. parent::setUp(array('uuid'));
  117. }
  118. /**
  119. * Tests uuid function calls.
  120. */
  121. public function testV5Function() {
  122. // DNS namespace UUID.
  123. $dns_namespace = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
  124. // Valid DNS generation test.
  125. $uuid = uuid_generate_v5($dns_namespace, 'drupal.org');
  126. $this->assertUuid($uuid, 'UUID for drupal.org is valid.');
  127. $this->assertEqual($uuid, 'c809fd30-48df-52e3-a9f2-2cd78129b8b1', 'UUID for drupal.org is correct.');
  128. // Invalid namespace test.
  129. $invalid_namespace = '01234567-c7a9-feda-27e5-75d00dabc123';
  130. $uuid = uuid_generate_v5($invalid_namespace, 'drupal.org');
  131. $this->assertFalse($uuid, 'Invalid namespace UUID rejected.');
  132. }
  133. }
  134. /**
  135. * Tests the Entity API functions.
  136. */
  137. class UUIDEntityTestCase extends UUIDTestCase {
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public static function getInfo() {
  142. return array(
  143. 'name' => 'Entity API functions',
  144. 'description' => 'Tests the Entity API functions.',
  145. 'group' => 'UUID',
  146. );
  147. }
  148. /**
  149. * {@inheritdoc}
  150. */
  151. protected function setUp() {
  152. parent::setUp(array('uuid'));
  153. }
  154. /**
  155. * Tests Entity API's UUID functions.
  156. */
  157. public function testEntityApiFunctions() {
  158. // Create some entities that we will work with.
  159. $user = $this->drupalCreateUser();
  160. $node = $this->drupalCreateNode(array('title' => 'original title', 'uid' => $user->uid));
  161. // Test entity_get_id_by_uuid().
  162. $nids = entity_get_id_by_uuid('node', array($node->uuid), FALSE);
  163. $this->assertTrue(in_array($node->nid, $nids), 'Lookup of entity ID works.');
  164. $vids = entity_get_id_by_uuid('node', array($node->vuuid), TRUE);
  165. $this->assertTrue(in_array($node->vid, $vids), 'Lookup of entity revision ID works.');
  166. // Test entity_get_uuid_by_id().
  167. $uuids = entity_get_uuid_by_id('node', array($node->nid), FALSE);
  168. $this->assertTrue(in_array($node->uuid, $uuids), 'Lookup of entity UUID works.');
  169. $vuuids = entity_get_uuid_by_id('node', array($node->vid), TRUE);
  170. $this->assertTrue(in_array($node->vuuid, $vuuids), 'Lookup of entity revision UUID works.');
  171. }
  172. }
  173. /**
  174. * Tests the User implementation.
  175. */
  176. class UUIDUserTestCase extends UUIDTestCase {
  177. /**
  178. * {@inheritdoc}
  179. */
  180. public static function getInfo() {
  181. return array(
  182. 'name' => 'User implementation',
  183. 'description' => 'Tests the User implementation.',
  184. 'group' => 'UUID',
  185. );
  186. }
  187. /**
  188. * {@inheritdoc}
  189. */
  190. protected function setUp() {
  191. $modules = array('uuid');
  192. // Some tests depends on the optional Entity API module.
  193. if (module_exists('entity')) {
  194. $modules[] = 'entity';
  195. }
  196. parent::setUp($modules);
  197. }
  198. /**
  199. * Test CRUD on users with UUID functions.
  200. */
  201. public function testUserCrud() {
  202. $user = $this->drupalCreateUser();
  203. $this->assertUuid($user->uuid, 'User UUID was generated.');
  204. // Test updating user.
  205. $user_test = clone $user;
  206. user_save($user_test, array('name' => 'new name'));
  207. $user_test = user_load($user->uid, TRUE);
  208. $this->assertEqual($user_test->uuid, $user->uuid, 'User UUID was intact after update.');
  209. // Test entity_uuid_load().
  210. $users = entity_uuid_load('user', array($user->uuid), array(), TRUE);
  211. $user_test = reset($users);
  212. $this->assertEqual($user_test->uid, $user->uid, 'User was correctly loaded with UUID.');
  213. // The following tests depends on the optional Entity API module.
  214. if (module_exists('entity')) {
  215. // Test entity_uuid_save() for users.
  216. $user_test = clone $user;
  217. $user_test->uid = rand();
  218. $user_test->name = 'new name';
  219. entity_uuid_save('user', $user_test);
  220. $user_test = user_load($user->uid, TRUE);
  221. $this->assertEqual($user_test->name, 'new name', 'Saving user with UUID mapped to correct user.');
  222. $this->assertEqual($user_test->uuid, $user->uuid, 'User UUID was intact after saving with UUID.');
  223. // Test entity_uuid_delete() for users.
  224. entity_uuid_delete('user', $user->uuid);
  225. $user_test = user_load($user->uid);
  226. $this->assertFalse($user_test, 'Deleting user with UUID worked.');
  227. }
  228. }
  229. }
  230. /**
  231. * Tests the Node implementation.
  232. */
  233. class UUIDNodeTestCase extends UUIDTestCase {
  234. /**
  235. * {@inheritdoc}
  236. */
  237. public static function getInfo() {
  238. return array(
  239. 'name' => 'Node implementation',
  240. 'description' => 'Tests the Node implementation.',
  241. 'group' => 'UUID',
  242. );
  243. }
  244. /**
  245. * {@inheritdoc}
  246. */
  247. protected function setUp() {
  248. $modules = array('uuid');
  249. // Some tests depends on the optional Entity API module.
  250. if (module_exists('entity')) {
  251. $modules[] = 'entity';
  252. }
  253. parent::setUp($modules);
  254. }
  255. /**
  256. * Tests CRUD on nodes with UUID functions.
  257. *
  258. * @todo
  259. * Break out into multiple test methods to loosen coupling between tests.
  260. */
  261. public function testNodeCrud() {
  262. // Create some entities that we will work with.
  263. $user = $this->drupalCreateUser();
  264. $node = $this->drupalCreateNode(array('title' => 'original title', 'uid' => $user->uid));
  265. $this->assertUuid($node->uuid, 'Node UUID was generated.');
  266. $this->assertUuid($node->vuuid, 'Node revision UUID was generated.');
  267. // Test node update, without creating new revision.
  268. $node_test = clone $node;
  269. $node_test->title = 'original title';
  270. $node_test->revision = FALSE;
  271. node_save($node_test);
  272. $node_test = node_load($node->nid, FALSE, TRUE);
  273. $this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after update, when not creating new revision.');
  274. $this->assertEqual($node_test->vuuid, $node->vuuid, 'Node revision UUID was intact after updating, when not creating new revision.');
  275. // Save the original revision IDs that we will test with later.
  276. $vid_old = $node_test->vid;
  277. $vuuid_old = $node_test->vuuid;
  278. $uuid_old = $node_test->uuid;
  279. // Test node update, with new revision.
  280. $node_test = clone $node;
  281. $node_test->title = 'newer title';
  282. $node_test->revision = TRUE;
  283. node_save($node_test);
  284. $node_test = node_load($node->nid, FALSE, TRUE);
  285. $this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after updating, when creating new revision.');
  286. $this->assertNotEqual($node_test->vuuid, $node->vuuid, 'A new node revision UUID was generated, when creating new revision.');
  287. $this->assertUuid($node_test->vuuid, 'The new node revision UUID was valid.');
  288. // Test entity_uuid_load().
  289. // Save some variables that we will test against.
  290. $nid_test = $node_test->nid;
  291. $vid_test = $node_test->vid;
  292. $uid_test = $user->uuid;
  293. $uuid_test = $node_test->uuid;
  294. $vuuid_test = $node_test->vuuid;
  295. $nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
  296. $node_test = reset($nodes);
  297. $this->assertEqual($node_test->nid, $nid_test, 'Node ID was correct when loading with UUID.');
  298. $this->assertEqual($node_test->vid, $vid_test, 'Node revision ID was correct when loading with UUID.');
  299. $this->assertEqual($node_test->uid, $uid_test, "Node author ID was transformed to UUID when loaded with UUID.");
  300. $this->assertEqual($node_test->uuid, $uuid_test, 'Node UUID was correct when loading with UUID.');
  301. $this->assertEqual($node_test->vuuid, $vuuid_test, 'Node revision UUID was correct when loading with UUID.');
  302. // Test entity_uuid_load() with conditions.
  303. // Load the previous revision UUID that we saved earlier.
  304. $nodes = entity_uuid_load('node', array($uuid_test), array('vuuid' => $vuuid_old));
  305. $node_test = reset($nodes);
  306. $this->assertTrue((($node_test->uuid == $uuid_test) && ($node_test->nid && $node->nid)), 'The correct entity was loaded when loading a universal entity with a revision UUID condition.');
  307. $this->assertEqual($node_test->vuuid, $vuuid_old, 'Correct revision UUID was loaded when loading a universal entity with a revision UUID condition.');
  308. $this->assertEqual($node_test->vid, $vid_old, 'Correct revision ID was loaded when loading a universal entity with a revision UUID condition.');
  309. $this->assertEqual($node_test->title, 'original title', 'Correct title was loaded when loading a universal entity with a revision UUID condition.');
  310. // The following tests depends on the optional Entity API module.
  311. if (module_exists('entity')) {
  312. // Reload the node again because we have created new revisions above.
  313. $node = node_load($node->nid, FALSE, TRUE);
  314. // Test entity_uuid_save() for nodes.
  315. $nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
  316. $node_test = reset($nodes);
  317. $node_test->nid = rand();
  318. $node_test->vid = rand();
  319. $node_test->title = 'new title';
  320. $node_test->revision = FALSE;
  321. entity_uuid_save('node', $node_test);
  322. $node_test = node_load($node->nid, FALSE, TRUE);
  323. $this->assertEqual($node_test->title, 'new title', 'Saving node with UUID mapped to correct node, when not creating new revision.');
  324. $this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after saving with UUID, when not creating new revision.');
  325. $this->assertEqual($node_test->vuuid, $node->vuuid, 'Node revison UUID was intact after saving with UUID, when not creating new revision.');
  326. $this->assertEqual($node_test->uid, $node->uid, "Node property 'uid' was intact after saving with UUID, when not creating new revision.");
  327. // Test the same thing again, but now triggering a new revision.
  328. $nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
  329. $node_test = reset($nodes);
  330. $node_test->nid = rand();
  331. $node_test->vid = rand();
  332. $node_test->title = 'newer title';
  333. $node_test->revision = TRUE;
  334. entity_uuid_save('node', $node_test);
  335. $node_test = node_load($node->nid, FALSE, TRUE);
  336. $this->assertEqual($node_test->title, 'newer title', 'Saving node with UUID mapped to correct node, when creating new revision.');
  337. $this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after saving with UUID, when creating new revision.');
  338. $this->assertNotEqual($node_test->vuuid, $node->vuuid, 'A new node revison UUID was generated after saving with UUID, when creating new revision.');
  339. $this->assertUuid($node_test->vuuid, 'New node revision UUID was valid.');
  340. $this->assertEqual($node_test->uid, $node->uid, "Node property 'uid' was intact after saving with UUID, when creating new revision.");
  341. // Test the same thing again, but now triggering a new revision from a
  342. // remote environment.
  343. // TODO: Move this test to the uuid_services module.
  344. $nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
  345. $node_test = reset($nodes);
  346. // Store the current local revision ID to test with later.
  347. $vid_old1 = $node_test->vid;
  348. // Simulate this node coming from a remote environment by generating
  349. // IDs that won't match. Only the UUID match at this point.
  350. $node_test->uuid_services = TRUE;
  351. $vuuid_test = uuid_generate();
  352. $node_test->nid = $nid_test;
  353. $node_test->vid = $vid_test;
  354. $node_test->vuuid = $vuuid_test;
  355. $node_test->revision = TRUE;
  356. entity_uuid_save('node', $node_test);
  357. $node_test = node_load($node->nid, FALSE, TRUE);
  358. $this->assertNotEqual($node_test->vid, $vid_old1, 'A new revision was created, when trying to create new revision with new revision UUID from remote site');
  359. $this->assertEqual($node_test->vuuid, $vuuid_test, 'The revison UUID was preserved after saving with UUID, when trying to create new revision with new revision UUID from remote site.');
  360. // Test the same thing again from a remote environment, but now with the
  361. // same vuuid as once previosuly. This should not trigger a new revision.
  362. // This covers the case of "dupe deployments" where a client might push a
  363. // node several times.
  364. // TODO: Move this test to the uuid_services module.
  365. $nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
  366. $node_test = reset($nodes);
  367. // Store the current local revision ID to test with later.
  368. $vid_old2 = $node_test->vid;
  369. // Simulate this node coming from a remote environment by generating
  370. // IDs that won't match.
  371. $node_test->uuid_services = TRUE;
  372. $node_test->nid = $nid_test;
  373. $node_test->vid = $vid_test;
  374. $node_test->vuuid = $vuuid_test;
  375. $node_test->revision = TRUE;
  376. entity_uuid_save('node', $node_test);
  377. $node_test = node_load($node->nid, FALSE, TRUE);
  378. $this->assertEqual($node_test->vid, $vid_old2, 'A new revision was not created, when trying to create new revision with existing revision UUID from remote site.');
  379. $this->assertEqual($node_test->vuuid, $vuuid_test, 'The revison UUID was preserved after saving with UUID, when trying to create new revision with existing revision UUID from remote site.');
  380. // Test the same this again, but now with an old revision.
  381. $nodes = entity_uuid_load('node', array($uuid_old), array('vuuid' => $vuuid_old), TRUE);
  382. $node_test = reset($nodes);
  383. // Simulate this node coming from a remote environment by generating
  384. // IDs that won't match.
  385. $node_test->uuid_services = TRUE;
  386. $node_test->nid = rand();
  387. $node_test->vid = rand();
  388. $node_test->revision = TRUE;
  389. $node_test->title = 'newest title';
  390. entity_uuid_save('node', $node_test);
  391. $node_test = node_load($node->nid, $vid_old, TRUE);
  392. $this->assertEqual($node_test->title, 'newest title', 'The revision was updated, when updating old revision with existing revision UUID from remote site.');
  393. $this->assertEqual($node_test->vuuid, $vuuid_old, 'The revison UUID was preserved after saving with UUID, when updating old revision with existing revision UUID from remote site.');
  394. // Setting the node options variable should also trigger a new revision.
  395. $nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
  396. $node_test = reset($nodes);
  397. variable_set('node_options_' . $node_test->type, array('revision'));
  398. entity_uuid_save('node', $node_test);
  399. $this->assertNotEqual($node_test->vuuid, $node->vuuid, 'A new node revison ID was generated after saving with UUID, when relying on the node options variable.');
  400. // Test entity_uuid_delete() for nodes.
  401. entity_uuid_delete('node', $node->uuid);
  402. $node_test = node_load($node->nid);
  403. $this->assertFalse($node_test, 'Deleting node with UUID worked.');
  404. }
  405. }
  406. }
  407. /**
  408. * Tests the Comment implementation.
  409. *
  410. * @todo
  411. * Contribute patch to CommentHelperCase::setUp() to make it extendable.
  412. */
  413. class UUIDCommentTestCase extends CommentHelperCase {
  414. /**
  415. * {@inheritdoc}
  416. */
  417. public static function getInfo() {
  418. return array(
  419. 'name' => 'Comment implementation',
  420. 'description' => 'Tests the Comment implementation.',
  421. 'group' => 'UUID',
  422. );
  423. }
  424. /**
  425. * Helper function that asserts a UUID.
  426. */
  427. protected function assertUuid($uuid, $message = NULL) {
  428. $this->assertTrue(uuid_is_valid($uuid), $message);
  429. }
  430. /**
  431. * Test CRUD on comments with UUID functions.
  432. */
  433. public function testCommentCrud() {
  434. // This is sub optimal, but due to how CommentHelperCase::setUp() is
  435. // constructed we are enforced to do this. So unfortunately this test
  436. // depends on 'entity' module for now.
  437. module_enable(array('uuid', 'entity'));
  438. $user = $this->drupalCreateUser();
  439. $this->drupalLogin($user);
  440. $node = $this->drupalCreateNode();
  441. $return = $this->postComment($node, 'Lorem ipsum');
  442. $comment = comment_load($return->id);
  443. $this->assertUuid($comment->uuid, 'Comment UUID was generated.');
  444. // Test updating comment.
  445. $comment_test = clone $comment;
  446. $comment_test->subject = 'new subject';
  447. comment_save($comment_test);
  448. $comment_test = comment_load($comment->cid);
  449. $this->assertEqual($comment_test->uuid, $comment->uuid, 'Comment UUID was intact after update.');
  450. // Test entity_uuid_load().
  451. $comments = entity_uuid_load('comment', array($comment->uuid), array(), TRUE);
  452. $comment_test = reset($comments);
  453. $this->assertEqual($comment_test->cid, $return->id, 'Comment was correctly loaded with UUID.');
  454. $this->assertEqual($comment_test->uid, $user->uuid, "Comment property 'uid' was transformed to UUID when loaded with UUID.");
  455. $this->assertEqual($comment_test->nid, $node->uuid, "Comment property 'nid' was transformed to UUID when loaded with UUID.");
  456. // The following tests depends on the optional Entity API module.
  457. if (module_exists('entity')) {
  458. // Test entity_uuid_save() for comments.
  459. $comments = entity_uuid_load('comment', array($comment->uuid), array(), TRUE);
  460. $comment_test = reset($comments);
  461. $comment_test->cid = rand();
  462. $comment_test->subject = 'newer subject';
  463. entity_uuid_save('comment', $comment_test);
  464. $comment_test = comment_load($comment->cid);
  465. $this->assertEqual($comment_test->subject, 'newer subject', 'Saving comment with UUID mapped to correct comment.');
  466. $this->assertEqual($comment_test->uuid, $comment->uuid, 'Comment UUID was intact after saving with UUID.');
  467. $this->assertEqual($comment_test->uid, $user->uid, "Comment property 'uid' was after saving with UUID.");
  468. $this->assertEqual($comment_test->nid, $node->nid, "Comment property 'nid' was after saving with UUID.");
  469. // Test entity_uuid_delete() for comments.
  470. entity_uuid_delete('comment', $comment->uuid);
  471. $comment_test = comment_load($comment->cid);
  472. $this->assertFalse($comment_test, 'Deleting comment with UUID worked.');
  473. }
  474. }
  475. }
  476. /**
  477. * Tests the Taxonomy implementation.
  478. */
  479. class UUIDTaxonomyTestCase extends TaxonomyWebTestCase {
  480. /**
  481. * {@inheritdoc}
  482. */
  483. public static function getInfo() {
  484. return array(
  485. 'name' => 'Taxonomy implementation',
  486. 'description' => 'Tests the Taxonomy implementation.',
  487. 'group' => 'UUID',
  488. );
  489. }
  490. /**
  491. * {@inheritdoc}
  492. *
  493. * A lot of code here is taken from TaxonomyTermTestCase::setUp().
  494. */
  495. protected function setUp() {
  496. $modules = array('taxonomy', 'uuid');
  497. // Some tests depends on the optional Entity API module.
  498. if (module_exists('entity')) {
  499. $modules[] = 'entity';
  500. }
  501. parent::setUp($modules);
  502. }
  503. /**
  504. * Helper function that asserts a UUID.
  505. */
  506. protected function assertUuid($uuid, $message = NULL) {
  507. $this->assertTrue(uuid_is_valid($uuid), $message);
  508. }
  509. /**
  510. * Test CRUD on comments with UUID functions.
  511. */
  512. public function testTaxonomyCrud() {
  513. $perms = array(
  514. 'administer taxonomy',
  515. 'administer nodes',
  516. 'bypass node access',
  517. );
  518. $user = $this->drupalCreateUser($perms);
  519. $this->drupalLogin($user);
  520. // Create a term by tagging a node. We'll use this node later too.
  521. $vocabulary = new stdClass();
  522. $vocabulary->vid = 1;
  523. $term = $this->createTerm($vocabulary);
  524. $this->assertUuid($term->uuid, 'Term UUID was generated.');
  525. // Test updating term.
  526. $term_test = clone $term;
  527. $term_test->name = 'new name';
  528. taxonomy_term_save($term_test);
  529. $term_test = taxonomy_term_load($term->tid);
  530. $this->assertEqual($term_test->uuid, $term->uuid, 'Term UUID was intact after update.');
  531. // Test entity_uuid_load().
  532. $terms = entity_uuid_load('taxonomy_term', array($term->uuid), array(), TRUE);
  533. $term_test = reset($terms);
  534. $this->assertEqual($term_test->tid, $term->tid, 'Term was correctly loaded with UUID.');
  535. // The following tests depends on the Entity API module.
  536. if (module_exists('entity')) {
  537. // Test entity_uuid_save() for terms.
  538. $terms = entity_uuid_load('taxonomy_term', array($term->uuid), array(), TRUE);
  539. $term_test = reset($terms);
  540. $term_test->tid = rand();
  541. $term_test->name = 'newer name';
  542. entity_uuid_save('taxonomy_term', $term_test);
  543. $term_test = taxonomy_term_load($term->tid);
  544. $this->assertEqual($term_test->name, 'newer name', 'Saving term with UUID mapped to correct term.');
  545. $this->assertEqual($term_test->uuid, $term->uuid, 'Term UUID was intact after saving with UUID.');
  546. // Test entity_uuid_delete() for nodes.
  547. entity_uuid_delete('taxonomy_term', $term->uuid);
  548. $term_test = taxonomy_term_load($term->tid);
  549. $this->assertFalse($term_test, 'Deleting term with UUID worked.');
  550. }
  551. }
  552. }
  553. /**
  554. * Tests for the UUID synchronization.
  555. */
  556. class UUIDSyncTestCase extends UUIDTestCase {
  557. /**
  558. * {@inheritdoc}
  559. */
  560. public static function getInfo() {
  561. return array(
  562. 'name' => 'UUID sync',
  563. 'description' => 'Tests the UUID synchronization.',
  564. 'group' => 'UUID',
  565. );
  566. }
  567. /**
  568. * Helper function that asserts that a database table column exists.
  569. *
  570. * @todo
  571. * There are something weird around this assertion.
  572. */
  573. protected function assertTableColumn($table, $column, $message) {
  574. $this->assertTrue(db_field_exists($table, $column), $message);
  575. }
  576. /**
  577. * Tests creating UUIDs for entities that don't have them.
  578. */
  579. public function testSync() {
  580. // These entities will not have UUID from the start, since the UUID module
  581. // isn't installed yet.
  582. $user = $this->drupalCreateUser();
  583. $node = $this->drupalCreateNode();
  584. $this->assertTrue(!isset($node->uuid), "Node has no UUID before installation of UUID module.");
  585. $this->assertTrue(!isset($node->vuuid), "Node has no revision UUID before installation of UUID module.");
  586. $this->assertTrue(!isset($user->uuid), "User has no UUID before installation of UUID module.");
  587. // Now enable the UUID module.
  588. module_enable(array('uuid'), TRUE);
  589. drupal_flush_all_caches();
  590. drupal_static_reset();
  591. // Check that the UUID column was generated for {node}.
  592. $this->assertTableColumn('node', 'uuid', 'UUID column was generated for the node table.');
  593. $this->assertTableColumn('node_revision', 'vuuid', 'Revision UUID column was generated for the node_revision table.');
  594. $this->assertTableColumn('users', 'uuid', 'UUID column was generated for the user table.');
  595. // Login with a user and click the sync button.
  596. $web_user = $this->drupalCreateUser(array('administer uuid'));
  597. $this->drupalLogin($web_user);
  598. $this->drupalPost('admin/config/system/uuid', array(), t('Create missing UUIDs'));
  599. // Test if UUID was generated for nodes.
  600. $node_test = node_load($node->nid, FALSE, TRUE);
  601. $this->assertUuid($node_test->uuid, 'Node UUID was generated when clicking the sync button.');
  602. $this->assertUuid($node_test->vuuid, 'Node revision UUID was generated when clicking the sync button.');
  603. // Test if UUID was generated for users.
  604. $user_test = user_load($user->uid, TRUE);
  605. $this->assertUuid($user_test->uuid, 'User UUID was generated when clicking the sync button.');
  606. }
  607. }