uuid.test 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. <?php
  2. /**
  3. * @file
  4. * Test suite for UUID module.
  5. */
  6. /**
  7. * Base class with some helper methods.
  8. */
  9. class UUIDTestCase extends DrupalWebTestCase {
  10. function setUp() {
  11. parent::setUp(func_get_args());
  12. }
  13. /**
  14. * Helper function that asserts a UUID.
  15. */
  16. function assertUUID($uuid, $message = NULL) {
  17. $this->assertTrue(uuid_is_valid($uuid), $message);
  18. }
  19. }
  20. /**
  21. * Tests the UUID API functions.
  22. */
  23. class UUIDAPITestCase extends UUIDTestCase {
  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. function setUp() {
  32. parent::setUp('uuid');
  33. }
  34. function testAPIFunctions() {
  35. // This is a valid UUID, we know that.
  36. $valid_uuid = '0ab26e6b-f074-4e44-9da6-1205fa0e9761';
  37. // Test the uuid_is_valid() function.
  38. $this->assertUUID($valid_uuid, 'UUID validation works.');
  39. // The default generator is 'php'.
  40. $uuid = uuid_generate();
  41. $this->assertUUID($uuid, 'PHP generator works.');
  42. // Test the 'mysql' generator.
  43. variable_set('uuid_generator', 'mysql');
  44. drupal_static_reset('uuid_generate');
  45. $uuid = uuid_generate();
  46. $this->assertUUID($uuid, 'MySQL generator works.');
  47. }
  48. }
  49. /**
  50. * Tests the Entity API functions.
  51. */
  52. class UUIDEntityTestCase extends UUIDTestCase {
  53. public static function getInfo() {
  54. return array(
  55. 'name' => 'Entity API functions',
  56. 'description' => 'Tests the Entity API functions.',
  57. 'group' => 'UUID',
  58. );
  59. }
  60. function setUp() {
  61. parent::setUp('uuid');
  62. }
  63. /**
  64. * Tests Entity API's UUID functions.
  65. */
  66. function testEntityAPIFunctions() {
  67. // Create some entities that we will work with.
  68. $user = $this->drupalCreateUser();
  69. $node = $this->drupalCreateNode(array('title' => 'original title', 'uid' => $user->uid));
  70. // Test entity_get_id_by_uuid().
  71. $nids = entity_get_id_by_uuid('node', array($node->uuid), FALSE);
  72. $this->assertTrue(in_array($node->nid, $nids), 'Lookup of entity ID works.');
  73. $vids = entity_get_id_by_uuid('node', array($node->vuuid), TRUE);
  74. $this->assertTrue(in_array($node->vid, $vids), 'Lookup of entity revision ID works.');
  75. // Test entity_get_uuid_by_id().
  76. $uuids = entity_get_uuid_by_id('node', array($node->nid), FALSE);
  77. $this->assertTrue(in_array($node->uuid, $uuids), 'Lookup of entity UUID works.');
  78. $vuuids = entity_get_uuid_by_id('node', array($node->vid), TRUE);
  79. $this->assertTrue(in_array($node->vuuid, $vuuids), 'Lookup of entity revision UUID works.');
  80. }
  81. }
  82. /**
  83. * Tests the User implementation.
  84. */
  85. class UUIDUserTestCase extends UUIDTestCase {
  86. public static function getInfo() {
  87. return array(
  88. 'name' => 'User implementation',
  89. 'description' => 'Tests the User implementation.',
  90. 'group' => 'UUID',
  91. );
  92. }
  93. function setUp() {
  94. // Some tests depends on the optional Entity API module.
  95. if (module_exists('entity')) {
  96. parent::setUp('uuid', 'entity');
  97. }
  98. else {
  99. parent::setUp('uuid');
  100. }
  101. }
  102. /**
  103. * Test CRUD on users with UUID functions.
  104. */
  105. function testUserCRUD() {
  106. $user = $this->drupalCreateUser();
  107. $this->assertUUID($user->uuid, 'User UUID was generated.');
  108. // Test updating user.
  109. $user_test = clone $user;
  110. user_save($user_test, array('name' => 'new name'));
  111. $user_test = user_load($user->uid, TRUE);
  112. $this->assertEqual($user_test->uuid, $user->uuid, 'User UUID was intact after update.');
  113. // Test entity_uuid_load().
  114. $users = entity_uuid_load('user', array($user->uuid), array(), TRUE);
  115. $user_test = reset($users);
  116. $this->assertEqual($user_test->uid, $user->uid, 'User was correctly loaded with UUID.');
  117. // The following tests depends on the optional Entity API module.
  118. if (module_exists('entity')) {
  119. // Test entity_uuid_save() for users.
  120. $user_test = clone $user;
  121. $user_test->uid = rand();
  122. $user_test->name = 'new name';
  123. entity_uuid_save('user', $user_test);
  124. $user_test = user_load($user->uid, TRUE);
  125. $this->assertEqual($user_test->name, 'new name', 'Saving user with UUID mapped to correct user.');
  126. $this->assertEqual($user_test->uuid, $user->uuid, 'User UUID was intact after saving with UUID.');
  127. // Test entity_uuid_delete() for users.
  128. entity_uuid_delete('user', $user->uuid);
  129. $user_test = user_load($user->uid);
  130. $this->assertFalse($user_test, 'Deleting user with UUID worked.');
  131. }
  132. }
  133. }
  134. /**
  135. * Tests the Node implementation.
  136. */
  137. class UUIDNodeTestCase extends UUIDTestCase {
  138. public static function getInfo() {
  139. return array(
  140. 'name' => 'Node implementation',
  141. 'description' => 'Tests the Node implementation.',
  142. 'group' => 'UUID',
  143. );
  144. }
  145. function setUp() {
  146. // Some tests depends on the optional Entity API module.
  147. if (module_exists('entity')) {
  148. parent::setUp('uuid', 'entity');
  149. }
  150. else {
  151. parent::setUp('uuid');
  152. }
  153. }
  154. /**
  155. * Tests CRUD on nodes with UUID functions.
  156. */
  157. function testNodeCRUD() {
  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. $this->assertUUID($node->uuid, 'Node UUID was generated.');
  162. $this->assertUUID($node->vuuid, 'Node revision UUID was generated.');
  163. // Test node update, without creating new revision.
  164. $node_test = clone $node;
  165. $node_test->title = 'new title';
  166. $node_test->revision = FALSE;
  167. node_save($node_test);
  168. $node_test = node_load($node->nid, FALSE, TRUE);
  169. $this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after update, when not creating new revision.');
  170. $this->assertEqual($node_test->vuuid, $node->vuuid, 'Node revision UUID was intact after updating, when not creating new revision.');
  171. // Test node update, with new revision.
  172. $node_test = clone $node;
  173. $node_test->title = 'newer title';
  174. $node_test->revision = TRUE;
  175. node_save($node_test);
  176. $node_test = node_load($node->nid, FALSE, TRUE);
  177. $this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after updating, when creating new revision.');
  178. $this->assertNotEqual($node_test->vuuid, $node->vuuid, 'A new node revision UUID was generated, when creating new revision.');
  179. $this->assertUUID($node_test->vuuid, 'The new node revision UUID was valid.');
  180. // Test entity_uuid_load().
  181. $nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
  182. $node_test = reset($nodes);
  183. $this->assertEqual($node_test->nid, $node->nid, 'Node was correctly loaded with UUID.');
  184. $this->assertEqual($node_test->uid, $user->uuid, "Node property 'uid' was transformed to UUID when loaded with UUID.");
  185. // The following tests depends on the optional Entity API module.
  186. if (module_exists('entity')) {
  187. // Reload the node again because we have created new revisions above.
  188. $node = node_load($node->nid, FALSE, TRUE);
  189. // Test entity_uuid_save() for nodes.
  190. $nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
  191. $node_test = reset($nodes);
  192. $node_test->nid = rand();
  193. $node_test->vid = rand();
  194. $node_test->title = 'new title';
  195. $node_test->revision = FALSE;
  196. entity_uuid_save('node', $node_test);
  197. $node_test = node_load($node->nid, FALSE, TRUE);
  198. $this->assertEqual($node_test->title, 'new title', 'Saving node with UUID mapped to correct node, when not creating new revision.');
  199. $this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after saving with UUID, when not creating new revision.');
  200. $this->assertEqual($node_test->vuuid, $node->vuuid, 'Node revison UUID was intact after saving with UUID, when not creating new revision.');
  201. $this->assertEqual($node_test->uid, $node->uid, "Node property 'uid' was intact after saving with UUID, when not creating new revision.");
  202. // Test the same thing again, but now triggering a new revision.
  203. $nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
  204. $node_test = reset($nodes);
  205. $node_test->nid = rand();
  206. $node_test->vid = rand();
  207. $node_test->title = 'newer title';
  208. $node_test->revision = TRUE;
  209. entity_uuid_save('node', $node_test);
  210. $node_test = node_load($node->nid, FALSE, TRUE);
  211. $this->assertEqual($node_test->title, 'newer title', 'Saving node with UUID mapped to correct node, when creating new revision.');
  212. $this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after saving with UUID, when creating new revision.');
  213. $this->assertNotEqual($node_test->vuuid, $node->vuuid, 'A new node revison UUID was generated after saving with UUID, when creating new revision.');
  214. $this->assertUUID($node_test->vuuid, 'New node revision UUID was valid.');
  215. $this->assertEqual($node_test->uid, $node->uid, "Node property 'uid' was intact after saving with UUID, when creating new revision.");
  216. // Test entity_uuid_delete() for nodes.
  217. entity_uuid_delete('node', $node->uuid);
  218. $node_test = node_load($node->nid);
  219. $this->assertFalse($node_test, 'Deleting node with UUID worked.');
  220. }
  221. }
  222. }
  223. /**
  224. * Tests the Comment implementation.
  225. *
  226. * @todo
  227. * Contribute patch to CommentHelperCase::setUp() to make it extendable.
  228. */
  229. class UUIDCommentTestCase extends CommentHelperCase {
  230. public static function getInfo() {
  231. return array(
  232. 'name' => 'Comment implementation',
  233. 'description' => 'Tests the Comment implementation.',
  234. 'group' => 'UUID',
  235. );
  236. }
  237. /**
  238. * Helper function that asserts a UUID.
  239. *
  240. * We have duplicated this function from UUIDTestCase since we have to extend
  241. * CommentHelperCase instead.
  242. */
  243. function assertUUID($uuid, $message = NULL) {
  244. $this->assertTrue(uuid_is_valid($uuid), $message);
  245. }
  246. /**
  247. * Test CRUD on comments with UUID functions.
  248. */
  249. function testCommentCRUD() {
  250. // This is sub optimal, but due to how CommentHelperCase::setUp() is
  251. // constructed we are enforced to do this. So unfortunately this test
  252. // depends on 'entity' module for now.
  253. module_enable(array('uuid', 'entity'), TRUE);
  254. $user = $this->drupalCreateUser();
  255. $this->drupalLogin($user);
  256. $node = $this->drupalCreateNode();
  257. $return = $this->postComment($node, 'Lorem ipsum');
  258. $comment = comment_load($return->id);
  259. $this->assertUUID($comment->uuid, 'Comment UUID was generated.');
  260. // Test updating comment.
  261. $comment_test = clone $comment;
  262. $comment_test->subject = 'new subject';
  263. comment_save($comment_test);
  264. $comment_test = comment_load($comment->cid);
  265. $this->assertEqual($comment_test->uuid, $comment->uuid, 'Comment UUID was intact after update.');
  266. // Test entity_uuid_load().
  267. $comments = entity_uuid_load('comment', array($comment->uuid), array(), TRUE);
  268. $comment_test = reset($comments);
  269. $this->assertEqual($comment_test->cid, $return->id, 'Comment was correctly loaded with UUID.');
  270. $this->assertEqual($comment_test->uid, $user->uuid, "Comment property 'uid' was transformed to UUID when loaded with UUID.");
  271. $this->assertEqual($comment_test->nid, $node->uuid, "Comment property 'nid' was transformed to UUID when loaded with UUID.");
  272. // The following tests depends on the optional Entity API module.
  273. if (module_exists('entity')) {
  274. // Test entity_uuid_save() for comments.
  275. $comments = entity_uuid_load('comment', array($comment->uuid), array(), TRUE);
  276. $comment_test = reset($comments);
  277. $comment_test->cid = rand();
  278. $comment_test->subject = 'newer subject';
  279. entity_uuid_save('comment', $comment_test);
  280. $comment_test = comment_load($comment->cid);
  281. $this->assertEqual($comment_test->subject, 'newer subject', 'Saving comment with UUID mapped to correct comment.');
  282. $this->assertEqual($comment_test->uuid, $comment->uuid, 'Comment UUID was intact after saving with UUID.');
  283. $this->assertEqual($comment_test->uid, $user->uid, "Comment property 'uid' was after saving with UUID.");
  284. $this->assertEqual($comment_test->nid, $node->nid, "Comment property 'nid' was after saving with UUID.");
  285. // Test entity_uuid_delete() for comments.
  286. entity_uuid_delete('comment', $comment->uuid);
  287. $comment_test = comment_load($comment->cid);
  288. $this->assertFalse($comment_test, 'Deleting comment with UUID worked.');
  289. }
  290. }
  291. }
  292. /**
  293. * Tests the Taxonomy implementation.
  294. */
  295. class UUIDTaxonomyTestCase extends TaxonomyWebTestCase {
  296. public static function getInfo() {
  297. return array(
  298. 'name' => 'Taxonomy implementation',
  299. 'description' => 'Tests the Taxonomy implementation.',
  300. 'group' => 'UUID',
  301. );
  302. }
  303. /**
  304. * A lot of code here is taken from TaxonomyTermTestCase::setUp().
  305. */
  306. function setUp() {
  307. // Some tests depends on the optional Entity API module.
  308. if (module_exists('entity')) {
  309. parent::setUp('taxonomy', 'uuid', 'entity');
  310. }
  311. else {
  312. parent::setUp('taxonomy', 'uuid');
  313. }
  314. }
  315. /**
  316. * Helper function that asserts a UUID.
  317. *
  318. * We have duplicated this function from UUIDTestCase since we have to extend
  319. * TaxonomyWebTestCase instead.
  320. */
  321. function assertUUID($uuid, $message = NULL) {
  322. $this->assertTrue(uuid_is_valid($uuid), $message);
  323. }
  324. /**
  325. * Test CRUD on comments with UUID functions.
  326. */
  327. function testTaxonomyCRUD() {
  328. $user = $this->drupalCreateUser(array('administer taxonomy', 'administer nodes', 'bypass node access'));
  329. $this->drupalLogin($user);
  330. // Create a term by tagging a node. We'll use this node later too.
  331. $vocabulary = new stdClass;
  332. $vocabulary->vid = 1;
  333. $term = $this->createTerm($vocabulary);
  334. $this->assertUUID($term->uuid, 'Term UUID was generated.');
  335. // Test updating term.
  336. $term_test = clone $term;
  337. $term_test->name = 'new name';
  338. taxonomy_term_save($term_test);
  339. $term_test = taxonomy_term_load($term->tid);
  340. $this->assertEqual($term_test->uuid, $term->uuid, 'Term UUID was intact after update.');
  341. // Test entity_uuid_load().
  342. $terms = entity_uuid_load('taxonomy_term', array($term->uuid), array(), TRUE);
  343. $term_test = reset($terms);
  344. $this->assertEqual($term_test->tid, $term->tid, 'Term was correctly loaded with UUID.');
  345. // The following tests depends on the Entity API module.
  346. if (module_exists('entity')) {
  347. // Test entity_uuid_save() for terms.
  348. $terms = entity_uuid_load('taxonomy_term', array($term->uuid), array(), TRUE);
  349. $term_test = reset($terms);
  350. $term_test->tid = rand();
  351. $term_test->name = 'newer name';
  352. entity_uuid_save('taxonomy_term', $term_test);
  353. $term_test = taxonomy_term_load($term->tid);
  354. $this->assertEqual($term_test->name, 'newer name', 'Saving term with UUID mapped to correct term.');
  355. $this->assertEqual($term_test->uuid, $term->uuid, 'Term UUID was intact after saving with UUID.');
  356. // Test entity_uuid_delete() for nodes.
  357. entity_uuid_delete('taxonomy_term', $term->uuid);
  358. $term_test = taxonomy_term_load($term->tid);
  359. $this->assertFalse($term_test, 'Deleting term with UUID worked.');
  360. }
  361. }
  362. }
  363. /**
  364. * Tests for the UUID synchronization.
  365. */
  366. class UUIDSyncTestCase extends UUIDTestCase {
  367. public static function getInfo() {
  368. return array(
  369. 'name' => 'UUID sync',
  370. 'description' => 'Tests the UUID synchronization.',
  371. 'group' => 'UUID',
  372. );
  373. }
  374. /**
  375. * Helper function that asserts that a database table column exists.
  376. *
  377. * @todo
  378. * There are something weird around this assertion.
  379. */
  380. function assertTableColumn($table, $column, $message) {
  381. $result = db_query("SHOW COLUMNS FROM {$table}");
  382. $exists = FALSE;
  383. foreach ($result as $record) {
  384. if ($record->field == $column) {
  385. $exists = TRUE;
  386. break;
  387. }
  388. }
  389. $this->assertTrue($exists, $message);
  390. }
  391. function testSync() {
  392. // These entities will not have UUID from the start, since the UUID module
  393. // isn't installed yet.
  394. $user = $this->drupalCreateUser();
  395. $node = $this->drupalCreateNode();
  396. $this->assertTrue(!isset($node->uuid), "Node has no UUID before installation of UUID module.");
  397. $this->assertTrue(!isset($node->vuuid), "Node has no revision UUID before installation of UUID module.");
  398. $this->assertTrue(!isset($user->uuid), "User has no UUID before installation of UUID module.");
  399. // Now enable the UUID module.
  400. module_enable(array('uuid'), TRUE);
  401. drupal_flush_all_caches();
  402. drupal_static_reset();
  403. // Check that the UUID column was generated for {node}.
  404. $this->assertTableColumn('node', 'uuid', 'UUID column was generated for the node table.');
  405. $this->assertTableColumn('node_revision', 'vuuid', 'Revision UUID column was generated for the node_revision table.');
  406. $this->assertTableColumn('users', 'uuid', 'UUID column was generated for the user table.');
  407. // Login with a user and click the sync button.
  408. $web_user = $this->drupalCreateUser(array('administer uuid'));
  409. $this->drupalLogin($web_user);
  410. $this->drupalPost('admin/config/system/uuid', array(), t('Create missing UUIDs'));
  411. // Test if UUID was generated for nodes.
  412. $node_test = node_load($node->nid, FALSE, TRUE);
  413. $this->assertUUID($node_test->uuid, 'Node UUID was generated when clicking the sync button.');
  414. $this->assertUUID($node_test->vuuid, 'Node revision UUID was generated when clicking the sync button.');
  415. // Test if UUID was generated for users.
  416. $user_test = user_load($user->uid, TRUE);
  417. $this->assertUUID($user_test->uuid, 'User UUID was generated when clicking the sync button.');
  418. }
  419. }
  420. class UUIDExportEntitiesWithDeploy extends DrupalWebTestCase {
  421. public static function getInfo() {
  422. return array(
  423. 'name' => 'Export UUID entities',
  424. 'description' => 'Test exporting UUID entities with Deploy and Features.',
  425. 'group' => 'UUID',
  426. );
  427. }
  428. function setUp() {
  429. parent::setUp('taxonomy', 'uuid', 'entity', 'features', 'deploy', 'deploy_example');
  430. }
  431. function testExport() {
  432. $test_user = $this->drupalCreateUser();
  433. $test_node = $this->drupalCreateNode(array(
  434. 'uid' => $test_user->uid,
  435. ));
  436. deploy_manager_add_to_plan('deploy_example_plan', 'node', $test_node);
  437. // TODO: Test the actual insert.
  438. $this->assertTrue(TRUE, 'Added a node with a user dependency to be exported as a Feature module.');
  439. // Login and recreate the example feature. The feature isn't installed. But
  440. // Features can still export the code, and we can test it.
  441. $web_user = $this->drupalCreateUser(array('administer features'));
  442. $this->drupalLogin($web_user);
  443. $code = $this->drupalPost('admin/structure/features/uuid_default_entities_example/recreate', array(), t('Download feature'));
  444. $this->assertTrue($code, 'Feature module was exported.');
  445. // Ensure that we find what we expect in the exported code.
  446. $node_test1 = preg_match('/' . $test_node->title . '/', $code);
  447. $node_test2 = preg_match("/'uri' => 'node\/" . $test_node->uuid . "'/", $code);
  448. $this->assertTrue($node_test1, 'Node title was found in the expoted code.');
  449. $this->assertTrue($node_test2, 'Node URI was found in the expoted code.');
  450. $user_test1 = preg_match('/' . $test_user->name . '/', $code);
  451. $user_test2 = preg_match("/'uri' => 'user\/" . $test_user->uuid . "'/", $code);
  452. $this->assertTrue($user_test1, 'User was found in the expoted code.');
  453. $this->assertTrue($user_test2, 'User URI was found in the expoted code.');
  454. }
  455. }
  456. /**
  457. * Tests for the UUID synchronization.
  458. */
  459. class UUIDImportEntitiesTestCase extends UUIDTestCase {
  460. /**
  461. * Representation of the UUIDs that is exported in our example feature, that
  462. * we use for testing.
  463. */
  464. public $term1_uuid = 'bcb92ce8-2236-e264-65c8-0c163ae716d1';
  465. public $term2_uuid = '4293a15c-531a-6164-7d1b-668ed019a6bd';
  466. public $term3_uuid = 'af738a46-f278-cf84-d94d-9e03879fd71e';
  467. public $node1_uuid = 'b0558664-c94b-3674-d9df-3e1696b2e471';
  468. public $node2_uuid = '5e3d8bbe-a1f2-f2d4-fdc0-71e6c23aa837';
  469. public $user1_uuid = '7cf875e6-dc15-4404-f190-5a7c3e91d14c';
  470. /**
  471. * Helper method to assert the uuid_entities component in any features.
  472. */
  473. function assertFeatureState($feature, $state, $message = '') {
  474. if (empty($message)) {
  475. switch ($state) {
  476. case FEATURES_DEFAULT:
  477. $readable_state = 'default';
  478. break;
  479. case FEATURES_OVERRIDDEN:
  480. $readable_state = 'overridden';
  481. break;
  482. default:
  483. $readable_state = 'unknown';
  484. break;
  485. }
  486. $message = format_string('%component in %feature had state: %state', array('%component' => 'uuid_entities', '%feature' => $feature, '%state' => $readable_state));
  487. }
  488. // Ensure that the features we used is in default state.
  489. $states = features_get_component_states(array($feature), TRUE, TRUE);
  490. if (!$this->assertEqual($states[$feature]['uuid_entities'], $state, $message)) {
  491. debug(format_string('Enabling functionality to show diff output for debug purposes.'));
  492. $success = module_enable(array('diff'));
  493. if ($success) {
  494. // Make sure we run on cold caches.
  495. drupal_flush_all_caches();
  496. drupal_static_reset();
  497. $user = $this->drupalCreateUser(array('administer features'));
  498. $this->drupalLogin($user);
  499. $this->drupalGet('admin/structure/features/' . $feature . '/diff');
  500. }
  501. else {
  502. debug(format_string('Download !module to see diff output for debug purposes.', array('!module' => 'diff.module')));
  503. }
  504. }
  505. }
  506. function getEntityByUuid($entity_type, $uuid) {
  507. $ids = entity_get_id_by_uuid($entity_type, array($uuid));
  508. $entities = entity_load($entity_type, $ids, NULL, TRUE);
  509. return reset($entities);
  510. }
  511. function enableFeature($feature) {
  512. $success = module_enable(array($feature), TRUE);
  513. $this->assertTrue($success, t('Enabled modules: %modules', array('%modules' => implode(', ', array($feature)))));
  514. // Make sure we run on cold caches.
  515. drupal_flush_all_caches();
  516. drupal_static_reset();
  517. }
  518. function revertFeature($feature) {
  519. features_revert(array($feature => array('uuid_entities')));
  520. $this->assertTrue(TRUE, format_string('Reverted feature: %feature', array('%feature' => $feature)));
  521. }
  522. function testImport() {
  523. $term1 = $this->getEntityByUuid('taxonomy_term', $this->term1_uuid);
  524. $term2 = $this->getEntityByUuid('taxonomy_term', $this->term2_uuid);
  525. $term3 = $this->getEntityByUuid('taxonomy_term', $this->term3_uuid);
  526. $node1 = $this->getEntityByUuid('node', $this->node1_uuid);
  527. $node2 = $this->getEntityByUuid('node', $this->node2_uuid);
  528. $user1 = $this->getEntityByUuid('user', $this->user1_uuid);
  529. // Ensure that we don't have our entities yet.
  530. $this->assertTrue(empty($term1), 'Term 1 has not been created yet.');
  531. $this->assertTrue(empty($term2), 'Term 2 has not been created yet.');
  532. $this->assertTrue(empty($term3), 'Term 3 has not been created yet.');
  533. $this->assertTrue(empty($node1), 'Node 1 has not been created yet.');
  534. $this->assertTrue(empty($node2), 'Node 2 has not been created yet.');
  535. $this->assertTrue(empty($user1), 'User 1 has not been created yet.');
  536. $this->enableFeature('uuid_default_entities_example');
  537. $term1 = $this->getEntityByUuid('taxonomy_term', $this->term1_uuid);
  538. $term2 = $this->getEntityByUuid('taxonomy_term', $this->term2_uuid);
  539. $term3 = $this->getEntityByUuid('taxonomy_term', $this->term3_uuid);
  540. $node1 = $this->getEntityByUuid('node', $this->node1_uuid);
  541. $node2 = $this->getEntityByUuid('node', $this->node2_uuid);
  542. $user1 = $this->getEntityByUuid('user', $this->user1_uuid);
  543. // Ensure that our entities was created.
  544. $this->assertEqual($term1->uuid, $this->term1_uuid, 'Term 1 was created.');
  545. $this->assertEqual($term2->uuid, $this->term2_uuid, 'Term 2 was created.');
  546. $this->assertEqual($term3->uuid, $this->term3_uuid, 'Term 3 was created.');
  547. $this->assertEqual($node1->uuid, $this->node1_uuid, 'Node 1 was created.');
  548. $this->assertEqual($node2->uuid, $this->node2_uuid, 'Node 2 was created.');
  549. $this->assertEqual($user1->uuid, $this->user1_uuid, 'User 1 was created.');
  550. // Check the features state.
  551. $this->assertFeatureState('uuid_default_entities_example', FEATURES_DEFAULT);
  552. // New property.
  553. $new = 'foo bar';
  554. // Change a term.
  555. $term1->name = $new;
  556. $status = taxonomy_term_save($term1);
  557. $this->assertEqual($status, SAVED_UPDATED, 'Updated term 1.');
  558. // Change a node.
  559. $node1->title = $new;
  560. node_save($node1);
  561. $this->assertEqual($node1->title, $new, 'Updated node 1.');
  562. // Change a user.
  563. $user1->name = $new;
  564. $updated_user = user_save($user1);
  565. $this->assertEqual($user1->name, $updated_user->name, 'Updated user 1.');
  566. // Check the features state.
  567. $this->assertFeatureState('uuid_default_entities_example', FEATURES_OVERRIDDEN);
  568. // Revert the feature.
  569. $this->revertFeature('uuid_default_entities_example');
  570. // Check the features state.
  571. $this->assertFeatureState('uuid_default_entities_example', FEATURES_DEFAULT);
  572. }
  573. }
  574. class UUIDImportEntitiesWithDeploy extends UUIDImportEntitiesTestCase {
  575. public static function getInfo() {
  576. return array(
  577. 'name' => 'Import UUID entities, with Deploy',
  578. 'description' => 'Test importing UUID entities with Features and Deploy.',
  579. 'group' => 'UUID',
  580. );
  581. }
  582. function setUp() {
  583. parent::setUp('taxonomy', 'uuid', 'entity', 'features', 'deploy', 'deploy_example');
  584. }
  585. }
  586. class UUIDImportEntitiesWithoutDeploy extends UUIDImportEntitiesTestCase {
  587. public static function getInfo() {
  588. return array(
  589. 'name' => 'Import UUID entities, without Deploy',
  590. 'description' => 'Test importing UUID entities with Features only.',
  591. 'group' => 'UUID',
  592. );
  593. }
  594. function setUp() {
  595. parent::setUp('taxonomy', 'uuid', 'entity', 'features');
  596. }
  597. }