uuid.test 26 KB

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