Révision 4eeb3b46
Ajouté par Assos Assos il y a presque 8 ans
drupal7/sites/all/modules/uuid/uuid.test | ||
---|---|---|
6 | 6 |
*/ |
7 | 7 |
|
8 | 8 |
/** |
9 |
* Base class with some helper methods. |
|
9 |
* UUID test helper trait. |
|
10 |
* |
|
11 |
* Contains methods that assist with running UUID tests. |
|
10 | 12 |
*/ |
11 |
class UUIDTestCase extends DrupalWebTestCase { |
|
12 |
|
|
13 |
/** |
|
14 |
* {@inheritdoc} |
|
15 |
*/ |
|
16 |
function setUp() { |
|
17 |
parent::setUp(func_get_args()); |
|
18 |
} |
|
13 |
trait UUIDTestHelper { |
|
19 | 14 |
|
20 | 15 |
/** |
21 | 16 |
* Helper function that asserts a UUID. |
22 | 17 |
*/ |
23 |
function assertUUID($uuid, $message = NULL) {
|
|
18 |
protected function assertUuid($uuid, $message = NULL) {
|
|
24 | 19 |
$this->assertTrue(uuid_is_valid($uuid), $message); |
25 | 20 |
} |
21 |
|
|
22 |
} |
|
23 |
|
|
24 |
/** |
|
25 |
* Base class with some helper methods. |
|
26 |
*/ |
|
27 |
abstract class UUIDTestCase extends DrupalWebTestCase { |
|
28 |
|
|
29 |
use UUIDTestHelper; |
|
30 |
|
|
26 | 31 |
} |
27 | 32 |
|
28 | 33 |
/** |
... | ... | |
44 | 49 |
/** |
45 | 50 |
* {@inheritdoc} |
46 | 51 |
*/ |
47 |
function setUp() { |
|
48 |
parent::setUp('uuid');
|
|
52 |
protected function setUp() {
|
|
53 |
parent::setUp(array('uuid'));
|
|
49 | 54 |
} |
50 | 55 |
|
51 | 56 |
/** |
52 | 57 |
* Tests uuid function calls. |
53 | 58 |
*/ |
54 |
function testAPIFunctions() {
|
|
59 |
public function testApiFunctions() {
|
|
55 | 60 |
// This is a valid UUID, we know that. |
56 | 61 |
$valid_uuid = '0ab26e6b-f074-4e44-9da6-1205fa0e9761'; |
57 | 62 |
// Test the uuid_is_valid() function. |
58 |
$this->assertUUID($valid_uuid, 'UUID validation works.');
|
|
63 |
$this->assertUuid($valid_uuid, 'UUID validation works.');
|
|
59 | 64 |
|
60 | 65 |
// The default generator is 'php'. |
61 | 66 |
$uuid = uuid_generate(); |
62 |
$this->assertUUID($uuid, 'PHP generator works.');
|
|
67 |
$this->assertUuid($uuid, 'PHP generator works.');
|
|
63 | 68 |
|
64 | 69 |
// Test the 'mysql' generator. |
65 | 70 |
variable_set('uuid_generator', 'mysql'); |
66 | 71 |
drupal_static_reset('uuid_generate'); |
67 | 72 |
$uuid = uuid_generate(); |
68 |
$this->assertUUID($uuid, 'MySQL generator works.'); |
|
73 |
$this->assertUuid($uuid, 'MySQL generator works.'); |
|
74 |
} |
|
75 |
|
|
76 |
/** |
|
77 |
* Checks that schema for tables of core entities is correctly defined. |
|
78 |
*/ |
|
79 |
public function testSchemas() { |
|
80 |
module_load_include('install', 'uuid'); |
|
81 |
|
|
82 |
$schemas = drupal_get_schema(); |
|
83 |
$field_info = uuid_schema_field_definition(); |
|
84 |
$key_names = array( |
|
85 |
'base table' => 'uuid', |
|
86 |
'revision table' => 'revision uuid', |
|
87 |
); |
|
88 |
|
|
89 |
foreach (uuid_get_core_entity_info() as $entity_info) { |
|
90 |
// Test the fields in "base" and "revision" tables. |
|
91 |
foreach ($key_names as $table_type => $key_name) { |
|
92 |
// Table or field is not defined in entity. |
|
93 |
if (!isset($entity_info[$table_type], $entity_info['entity keys'][$key_name])) { |
|
94 |
// Not all entities have a revisions table. |
|
95 |
continue; |
|
96 |
} |
|
97 |
|
|
98 |
$field_name = $entity_info['entity keys'][$key_name]; |
|
99 |
$table_name = $entity_info[$table_type]; |
|
100 |
|
|
101 |
if (!isset($schemas[$table_name])) { |
|
102 |
$this->fail(sprintf('Database schema does not have a "%s" table.', $table_name)); |
|
103 |
continue; |
|
104 |
} |
|
105 |
|
|
106 |
$properties = array( |
|
107 |
'field' => array('fields', $field_info), |
|
108 |
'index' => array('indexes', array($field_name)), |
|
109 |
); |
|
110 |
|
|
111 |
// Check integrity of the field and index definition. |
|
112 |
foreach ($properties as $type => $data) { |
|
113 |
list($property, $value) = $data; |
|
114 |
|
|
115 |
$message = sprintf('Definition of the "%s" %s in the "%s" schema', $field_name, $type, $table_name); |
|
116 |
|
|
117 |
if (isset($schemas[$table_name][$property][$field_name])) { |
|
118 |
$this->assertIdentical($schemas[$table_name][$property][$field_name], $value, "$message is correct."); |
|
119 |
} |
|
120 |
else { |
|
121 |
$this->fail("$message does not exist."); |
|
122 |
} |
|
123 |
} |
|
124 |
} |
|
125 |
} |
|
126 |
} |
|
127 |
|
|
128 |
} |
|
129 |
|
|
130 |
/** |
|
131 |
* Tests the UUID API functions. |
|
132 |
*/ |
|
133 |
class UUIDV5TestCase extends UUIDTestCase { |
|
134 |
|
|
135 |
/** |
|
136 |
* {@inheritdoc} |
|
137 |
*/ |
|
138 |
public static function getInfo() { |
|
139 |
return array( |
|
140 |
'name' => 'UUID v5', |
|
141 |
'description' => 'Tests the UUID v5 function.', |
|
142 |
'group' => 'UUID', |
|
143 |
); |
|
144 |
} |
|
145 |
|
|
146 |
/** |
|
147 |
* {@inheritdoc} |
|
148 |
*/ |
|
149 |
protected function setUp() { |
|
150 |
parent::setUp(array('uuid')); |
|
151 |
} |
|
152 |
|
|
153 |
/** |
|
154 |
* Tests uuid function calls. |
|
155 |
*/ |
|
156 |
public function testV5Function() { |
|
157 |
// DNS namespace UUID. |
|
158 |
$dns_namespace = '6ba7b810-9dad-11d1-80b4-00c04fd430c8'; |
|
159 |
|
|
160 |
// Valid DNS generation test. |
|
161 |
$uuid = uuid_generate_v5($dns_namespace, 'drupal.org'); |
|
162 |
$this->assertUuid($uuid, 'UUID for drupal.org is valid.'); |
|
163 |
$this->assertEqual($uuid, 'c809fd30-48df-52e3-a9f2-2cd78129b8b1', 'UUID for drupal.org is correct.'); |
|
164 |
|
|
165 |
// Invalid namespace test. |
|
166 |
$invalid_namespace = '01234567-c7a9-feda-27e5-75d00dabc123'; |
|
167 |
$uuid = uuid_generate_v5($invalid_namespace, 'drupal.org'); |
|
168 |
$this->assertFalse($uuid, 'Invalid namespace UUID rejected.'); |
|
69 | 169 |
} |
170 |
|
|
70 | 171 |
} |
71 | 172 |
|
72 | 173 |
/** |
... | ... | |
88 | 189 |
/** |
89 | 190 |
* {@inheritdoc} |
90 | 191 |
*/ |
91 |
function setUp() { |
|
92 |
parent::setUp('uuid');
|
|
192 |
protected function setUp() {
|
|
193 |
parent::setUp(array('uuid'));
|
|
93 | 194 |
} |
94 | 195 |
|
95 | 196 |
/** |
96 | 197 |
* Tests Entity API's UUID functions. |
97 | 198 |
*/ |
98 |
function testEntityAPIFunctions() {
|
|
199 |
public function testEntityApiFunctions() {
|
|
99 | 200 |
// Create some entities that we will work with. |
100 | 201 |
$user = $this->drupalCreateUser(); |
101 | 202 |
$node = $this->drupalCreateNode(array('title' => 'original title', 'uid' => $user->uid)); |
... | ... | |
112 | 213 |
$vuuids = entity_get_uuid_by_id('node', array($node->vid), TRUE); |
113 | 214 |
$this->assertTrue(in_array($node->vuuid, $vuuids), 'Lookup of entity revision UUID works.'); |
114 | 215 |
} |
216 |
|
|
115 | 217 |
} |
116 | 218 |
|
117 | 219 |
/** |
... | ... | |
133 | 235 |
/** |
134 | 236 |
* {@inheritdoc} |
135 | 237 |
*/ |
136 |
function setUp() { |
|
238 |
protected function setUp() { |
|
239 |
$modules = array('uuid'); |
|
240 |
|
|
137 | 241 |
// Some tests depends on the optional Entity API module. |
138 | 242 |
if (module_exists('entity')) { |
139 |
parent::setUp('uuid', 'entity'); |
|
140 |
} |
|
141 |
else { |
|
142 |
parent::setUp('uuid'); |
|
243 |
$modules[] = 'entity'; |
|
143 | 244 |
} |
245 |
|
|
246 |
parent::setUp($modules); |
|
144 | 247 |
} |
145 | 248 |
|
146 | 249 |
/** |
147 | 250 |
* Test CRUD on users with UUID functions. |
148 | 251 |
*/ |
149 |
function testUserCRUD() {
|
|
252 |
public function testUserCrud() {
|
|
150 | 253 |
$user = $this->drupalCreateUser(); |
151 |
$this->assertUUID($user->uuid, 'User UUID was generated.');
|
|
254 |
$this->assertUuid($user->uuid, 'User UUID was generated.');
|
|
152 | 255 |
|
153 | 256 |
// Test updating user. |
154 | 257 |
$user_test = clone $user; |
... | ... | |
178 | 281 |
$this->assertFalse($user_test, 'Deleting user with UUID worked.'); |
179 | 282 |
} |
180 | 283 |
} |
284 |
|
|
181 | 285 |
} |
182 | 286 |
|
183 | 287 |
/** |
... | ... | |
199 | 303 |
/** |
200 | 304 |
* {@inheritdoc} |
201 | 305 |
*/ |
202 |
function setUp() { |
|
306 |
protected function setUp() { |
|
307 |
$modules = array('uuid'); |
|
308 |
|
|
203 | 309 |
// Some tests depends on the optional Entity API module. |
204 | 310 |
if (module_exists('entity')) { |
205 |
parent::setUp('uuid', 'entity'); |
|
206 |
} |
|
207 |
else { |
|
208 |
parent::setUp('uuid'); |
|
311 |
$modules[] = 'entity'; |
|
209 | 312 |
} |
313 |
|
|
314 |
parent::setUp($modules); |
|
210 | 315 |
} |
211 | 316 |
|
212 | 317 |
/** |
... | ... | |
215 | 320 |
* @todo |
216 | 321 |
* Break out into multiple test methods to loosen coupling between tests. |
217 | 322 |
*/ |
218 |
function testNodeCRUD() {
|
|
323 |
public function testNodeCrud() {
|
|
219 | 324 |
// Create some entities that we will work with. |
220 | 325 |
$user = $this->drupalCreateUser(); |
221 | 326 |
$node = $this->drupalCreateNode(array('title' => 'original title', 'uid' => $user->uid)); |
222 | 327 |
|
223 |
$this->assertUUID($node->uuid, 'Node UUID was generated.');
|
|
224 |
$this->assertUUID($node->vuuid, 'Node revision UUID was generated.');
|
|
328 |
$this->assertUuid($node->uuid, 'Node UUID was generated.');
|
|
329 |
$this->assertUuid($node->vuuid, 'Node revision UUID was generated.');
|
|
225 | 330 |
|
226 | 331 |
// Test node update, without creating new revision. |
227 | 332 |
$node_test = clone $node; |
... | ... | |
244 | 349 |
$node_test = node_load($node->nid, FALSE, TRUE); |
245 | 350 |
$this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after updating, when creating new revision.'); |
246 | 351 |
$this->assertNotEqual($node_test->vuuid, $node->vuuid, 'A new node revision UUID was generated, when creating new revision.'); |
247 |
$this->assertUUID($node_test->vuuid, 'The new node revision UUID was valid.');
|
|
352 |
$this->assertUuid($node_test->vuuid, 'The new node revision UUID was valid.');
|
|
248 | 353 |
|
249 | 354 |
// Test entity_uuid_load(). |
250 | 355 |
// Save some variables that we will test against. |
... | ... | |
300 | 405 |
$this->assertEqual($node_test->title, 'newer title', 'Saving node with UUID mapped to correct node, when creating new revision.'); |
301 | 406 |
$this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after saving with UUID, when creating new revision.'); |
302 | 407 |
$this->assertNotEqual($node_test->vuuid, $node->vuuid, 'A new node revison UUID was generated after saving with UUID, when creating new revision.'); |
303 |
$this->assertUUID($node_test->vuuid, 'New node revision UUID was valid.');
|
|
408 |
$this->assertUuid($node_test->vuuid, 'New node revision UUID was valid.');
|
|
304 | 409 |
$this->assertEqual($node_test->uid, $node->uid, "Node property 'uid' was intact after saving with UUID, when creating new revision."); |
305 | 410 |
|
306 | 411 |
// Test the same thing again, but now triggering a new revision from a |
... | ... | |
310 | 415 |
$node_test = reset($nodes); |
311 | 416 |
// Store the current local revision ID to test with later. |
312 | 417 |
$vid_old1 = $node_test->vid; |
313 |
$vuuid_old1 = $node_test->vuuid; |
|
314 | 418 |
// Simulate this node coming from a remote environment by generating |
315 | 419 |
// IDs that won't match. Only the UUID match at this point. |
316 | 420 |
$node_test->uuid_services = TRUE; |
317 |
$nid_remote = rand(); |
|
318 |
$vid_remote = rand(); |
|
319 | 421 |
$vuuid_test = uuid_generate(); |
320 | 422 |
$node_test->nid = $nid_test; |
321 | 423 |
$node_test->vid = $vid_test; |
... | ... | |
375 | 477 |
$this->assertFalse($node_test, 'Deleting node with UUID worked.'); |
376 | 478 |
} |
377 | 479 |
} |
480 |
|
|
378 | 481 |
} |
379 | 482 |
|
380 | 483 |
/** |
... | ... | |
385 | 488 |
*/ |
386 | 489 |
class UUIDCommentTestCase extends CommentHelperCase { |
387 | 490 |
|
491 |
use UUIDTestHelper; |
|
492 |
|
|
388 | 493 |
/** |
389 | 494 |
* {@inheritdoc} |
390 | 495 |
*/ |
... | ... | |
396 | 501 |
); |
397 | 502 |
} |
398 | 503 |
|
399 |
/** |
|
400 |
* Helper function that asserts a UUID. |
|
401 |
* |
|
402 |
* We have duplicated this function from UUIDTestCase since we have to extend |
|
403 |
* CommentHelperCase instead. |
|
404 |
*/ |
|
405 |
function assertUUID($uuid, $message = NULL) { |
|
406 |
$this->assertTrue(uuid_is_valid($uuid), $message); |
|
407 |
} |
|
408 |
|
|
409 | 504 |
/** |
410 | 505 |
* Test CRUD on comments with UUID functions. |
411 | 506 |
*/ |
412 |
function testCommentCRUD() {
|
|
507 |
public function testCommentCrud() {
|
|
413 | 508 |
// This is sub optimal, but due to how CommentHelperCase::setUp() is |
414 | 509 |
// constructed we are enforced to do this. So unfortunately this test |
415 | 510 |
// depends on 'entity' module for now. |
416 |
module_enable(array('uuid', 'entity'), TRUE);
|
|
511 |
module_enable(array('uuid', 'entity')); |
|
417 | 512 |
$user = $this->drupalCreateUser(); |
418 | 513 |
$this->drupalLogin($user); |
419 | 514 |
$node = $this->drupalCreateNode(); |
420 | 515 |
$return = $this->postComment($node, 'Lorem ipsum'); |
421 | 516 |
|
422 | 517 |
$comment = comment_load($return->id); |
423 |
$this->assertUUID($comment->uuid, 'Comment UUID was generated.');
|
|
518 |
$this->assertUuid($comment->uuid, 'Comment UUID was generated.');
|
|
424 | 519 |
|
425 | 520 |
// Test updating comment. |
426 | 521 |
$comment_test = clone $comment; |
... | ... | |
456 | 551 |
$this->assertFalse($comment_test, 'Deleting comment with UUID worked.'); |
457 | 552 |
} |
458 | 553 |
} |
554 |
|
|
459 | 555 |
} |
460 | 556 |
|
461 | 557 |
/** |
... | ... | |
463 | 559 |
*/ |
464 | 560 |
class UUIDTaxonomyTestCase extends TaxonomyWebTestCase { |
465 | 561 |
|
562 |
use UUIDTestHelper; |
|
563 |
|
|
466 | 564 |
/** |
467 | 565 |
* {@inheritdoc} |
468 | 566 |
*/ |
... | ... | |
479 | 577 |
* |
480 | 578 |
* A lot of code here is taken from TaxonomyTermTestCase::setUp(). |
481 | 579 |
*/ |
482 |
function setUp() { |
|
580 |
protected function setUp() { |
|
581 |
$modules = array('taxonomy', 'uuid'); |
|
582 |
|
|
483 | 583 |
// Some tests depends on the optional Entity API module. |
484 | 584 |
if (module_exists('entity')) { |
485 |
parent::setUp('taxonomy', 'uuid', 'entity'); |
|
486 |
} |
|
487 |
else { |
|
488 |
parent::setUp('taxonomy', 'uuid'); |
|
585 |
$modules[] = 'entity'; |
|
489 | 586 |
} |
490 |
} |
|
491 | 587 |
|
492 |
/** |
|
493 |
* Helper function that asserts a UUID. |
|
494 |
* |
|
495 |
* We have duplicated this function from UUIDTestCase since we have to extend |
|
496 |
* TaxonomyWebTestCase instead. |
|
497 |
*/ |
|
498 |
function assertUUID($uuid, $message = NULL) { |
|
499 |
$this->assertTrue(uuid_is_valid($uuid), $message); |
|
588 |
parent::setUp($modules); |
|
500 | 589 |
} |
501 | 590 |
|
502 | 591 |
/** |
503 | 592 |
* Test CRUD on comments with UUID functions. |
504 | 593 |
*/ |
505 |
function testTaxonomyCRUD() { |
|
506 |
$user = $this->drupalCreateUser(array('administer taxonomy', 'administer nodes', 'bypass node access')); |
|
594 |
public function testTaxonomyCrud() { |
|
595 |
$perms = array( |
|
596 |
'administer taxonomy', |
|
597 |
'administer nodes', |
|
598 |
'bypass node access', |
|
599 |
); |
|
600 |
$user = $this->drupalCreateUser($perms); |
|
507 | 601 |
$this->drupalLogin($user); |
508 | 602 |
|
509 | 603 |
// Create a term by tagging a node. We'll use this node later too. |
510 |
$vocabulary = new stdClass; |
|
604 |
$vocabulary = new stdClass();
|
|
511 | 605 |
$vocabulary->vid = 1; |
512 | 606 |
$term = $this->createTerm($vocabulary); |
513 |
$this->assertUUID($term->uuid, 'Term UUID was generated.');
|
|
607 |
$this->assertUuid($term->uuid, 'Term UUID was generated.');
|
|
514 | 608 |
|
515 | 609 |
// Test updating term. |
516 | 610 |
$term_test = clone $term; |
... | ... | |
542 | 636 |
$this->assertFalse($term_test, 'Deleting term with UUID worked.'); |
543 | 637 |
} |
544 | 638 |
} |
639 |
|
|
545 | 640 |
} |
546 | 641 |
|
547 | 642 |
/** |
... | ... | |
566 | 661 |
* @todo |
567 | 662 |
* There are something weird around this assertion. |
568 | 663 |
*/ |
569 |
function assertTableColumn($table, $column, $message) { |
|
664 |
protected function assertTableColumn($table, $column, $message) {
|
|
570 | 665 |
$this->assertTrue(db_field_exists($table, $column), $message); |
571 | 666 |
} |
572 | 667 |
|
573 | 668 |
/** |
574 | 669 |
* Tests creating UUIDs for entities that don't have them. |
575 | 670 |
*/ |
576 |
function testSync() { |
|
671 |
public function testSync() {
|
|
577 | 672 |
// These entities will not have UUID from the start, since the UUID module |
578 | 673 |
// isn't installed yet. |
579 | 674 |
$user = $this->drupalCreateUser(); |
... | ... | |
600 | 695 |
|
601 | 696 |
// Test if UUID was generated for nodes. |
602 | 697 |
$node_test = node_load($node->nid, FALSE, TRUE); |
603 |
$this->assertUUID($node_test->uuid, 'Node UUID was generated when clicking the sync button.');
|
|
604 |
$this->assertUUID($node_test->vuuid, 'Node revision UUID was generated when clicking the sync button.');
|
|
698 |
$this->assertUuid($node_test->uuid, 'Node UUID was generated when clicking the sync button.');
|
|
699 |
$this->assertUuid($node_test->vuuid, 'Node revision UUID was generated when clicking the sync button.');
|
|
605 | 700 |
|
606 | 701 |
// Test if UUID was generated for users. |
607 | 702 |
$user_test = user_load($user->uid, TRUE); |
608 |
$this->assertUUID($user_test->uuid, 'User UUID was generated when clicking the sync button.');
|
|
703 |
$this->assertUuid($user_test->uuid, 'User UUID was generated when clicking the sync button.');
|
|
609 | 704 |
} |
705 |
|
|
610 | 706 |
} |
Formats disponibles : Unified diff
Weekly update of contrib modules