SqlContentEntityStorageSchemaTest.php 39.7 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\Tests\Core\Entity\Sql\SqlContentEntityStorageSchemaTest.
6 7
 */

8
namespace Drupal\Tests\Core\Entity\Sql;
9 10 11

use Drupal\Core\Entity\ContentEntityType;
use Drupal\Core\Entity\Sql\DefaultTableMapping;
12
use Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema;
13 14 15
use Drupal\Tests\UnitTestCase;

/**
16
 * @coversDefaultClass \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema
17 18
 * @group Entity
 */
19
class SqlContentEntityStorageSchemaTest extends UnitTestCase {
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

  /**
   * The mocked entity manager used in this test.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityManager;

  /**
   * The mocked entity type used in this test.
   *
   * @var \Drupal\Core\Entity\ContentEntityTypeInterface
   */
  protected $entityType;

  /**
   * The mocked SQL storage used in this test.
   *
38
   * @var \Drupal\Core\Entity\Sql\SqlContentEntityStorage|\PHPUnit_Framework_MockObject_MockObject
39 40 41 42 43 44 45 46 47 48 49
   */
  protected $storage;

  /**
   * The mocked field definitions used in this test.
   *
   * @var \Drupal\Core\Field\FieldStorageDefinitionInterface[]|\PHPUnit_Framework_MockObject_MockObject[]
   */
  protected $storageDefinitions;

  /**
50
   * The storage schema handler used in this test.
51
   *
52
   * @var \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema.
53
   */
54
  protected $storageSchema;
55 56 57 58

  /**
   * {@inheritdoc}
   */
59
  protected function setUp() {
60
    $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
61
    $this->storage = $this->getMockBuilder('Drupal\Core\Entity\Sql\SqlContentEntityStorage')
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
      ->disableOriginalConstructor()
      ->getMock();

    $this->storage->expects($this->any())
      ->method('getBaseTable')
      ->will($this->returnValue('entity_test'));

    // Add an ID field. This also acts as a test for a simple, single-column
    // field.
    $this->setUpStorageDefinition('id', array(
      'columns' => array(
        'value' => array(
          'type' => 'int',
        ),
      ),
    ));
  }

  /**
   * Tests the schema for non-revisionable, non-translatable entities.
   *
83 84 85 86 87 88 89 90 91 92 93 94
   * @covers ::__construct
   * @covers ::getEntitySchemaTables
   * @covers ::initializeBaseTable
   * @covers ::addTableDefaults
   * @covers ::getEntityIndexName
   * @covers ::getFieldIndexes
   * @covers ::getFieldUniqueKeys
   * @covers ::getFieldForeignKeys
   * @covers ::getFieldSchemaData
   * @covers ::addDefaultLangcodeSchema
   * @covers ::processBaseTable
   * @covers ::processIdentifierSchema
95
   */
96
  public function testGetSchemaBase() {
97 98
    $this->entityType = new ContentEntityType(array(
      'id' => 'entity_test',
99
      'entity_keys' => array('id' => 'id'),
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    ));

    // Add a field with a 'length' constraint.
    $this->setUpStorageDefinition('name', array(
      'columns' => array(
        'value' => array(
          'type' => 'varchar',
          'length' => 255,
        ),
      ),
    ));
    // Add a multi-column field.
    $this->setUpStorageDefinition('description', array(
      'columns' => array(
        'value' => array(
          'type' => 'text',
          'description' => 'The text value',
        ),
        'format' => array(
          'type' => 'varchar',
          'description' => 'The text description',
        ),
      ),
    ));
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    // Add a field with a unique key.
    $this->setUpStorageDefinition('uuid', array(
      'columns' => array(
        'value' => array(
          'type' => 'varchar',
          'length' => 128,
        ),
      ),
      'unique keys' => array(
        'value' => array('value'),
      ),
    ));
    // Add a field with a unique key, specified as column name and length.
    $this->setUpStorageDefinition('hash', array(
      'columns' => array(
        'value' => array(
          'type' => 'varchar',
          'length' => 20,
        ),
      ),
      'unique keys' => array(
        'value' => array(array('value', 10)),
      ),
    ));
    // Add a field with a multi-column unique key.
    $this->setUpStorageDefinition('email', array(
      'columns' => array(
        'username' => array(
          'type' => 'varchar',
        ),
        'hostname' => array(
          'type' => 'varchar',
        ),
        'domain' => array(
          'type' => 'varchar',
        )
      ),
      'unique keys' => array(
        'email' => array('username', 'hostname', array('domain', 3)),
      ),
    ));
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
    // Add a field with an index.
    $this->setUpStorageDefinition('owner', array(
      'columns' => array(
        'target_id' => array(
          'type' => 'int',
        ),
      ),
      'indexes' => array(
        'target_id' => array('target_id'),
      ),
    ));
    // Add a field with an index, specified as column name and length.
    $this->setUpStorageDefinition('translator', array(
      'columns' => array(
        'target_id' => array(
          'type' => 'int',
        ),
      ),
      'indexes' => array(
        'target_id' => array(array('target_id', 10)),
      ),
    ));
    // Add a field with a multi-column index.
    $this->setUpStorageDefinition('location', array(
      'columns' => array(
        'country' => array(
          'type' => 'varchar',
        ),
        'state' => array(
          'type' => 'varchar',
        ),
        'city' => array(
          'type' => 'varchar',
        )
      ),
      'indexes' => array(
        'country_state_city' => array('country', 'state', array('city', 10)),
      ),
    ));
    // Add a field with a foreign key.
    $this->setUpStorageDefinition('editor', array(
      'columns' => array(
        'target_id' => array(
          'type' => 'int',
        ),
      ),
      'foreign keys' => array(
        'user_id' => array(
          'table' => 'users',
          'columns' => array('target_id' => 'uid'),
        ),
      ),
    ));
    // Add a multi-column field with a foreign key.
    $this->setUpStorageDefinition('editor_revision', array(
      'columns' => array(
        'target_id' => array(
          'type' => 'int',
        ),
        'target_revision_id' => array(
          'type' => 'int',
        ),
      ),
      'foreign keys' => array(
        'user_id' => array(
          'table' => 'users',
          'columns' => array('target_id' => 'uid'),
        ),
      ),
    ));
235 236 237 238 239 240 241 242 243 244 245
    // Add a field with a really long index.
    $this->setUpStorageDefinition('long_index_name', array(
      'columns' => array(
        'long_index_name' => array(
          'type' => 'int',
        ),
      ),
      'indexes' => array(
        'long_index_name_really_long_long_name' => array(array('long_index_name', 10)),
      ),
    ));
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268

    $expected = array(
      'entity_test' => array(
        'description' => 'The base table for entity_test entities.',
        'fields' => array(
          'id' => array(
            'description' => 'The id field.',
            'type' => 'serial',
            'not null' => TRUE,
          ),
          'name' => array(
            'description' => 'The name field.',
            'type' => 'varchar',
            'length' => 255,
          ),
          'description__value' => array(
            'description' => 'The description field.',
            'type' => 'text',
          ),
          'description__format' => array(
            'description' => 'The description field.',
            'type' => 'varchar',
          ),
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
          'uuid' => array(
            'description' => 'The uuid field.',
            'type' => 'varchar',
            'length' => 128,
          ),
          'hash' => array(
            'description' => 'The hash field.',
            'type' => 'varchar',
            'length' => 20,
          ),
          'email__username' => array(
            'description' => 'The email field.',
            'type' => 'varchar',
          ),
          'email__hostname' => array(
            'description' => 'The email field.',
            'type' => 'varchar',
          ),
          'email__domain' => array(
            'description' => 'The email field.',
            'type' => 'varchar',
          ),
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
          'owner' => array(
            'description' => 'The owner field.',
            'type' => 'int',
          ),
          'translator' => array(
            'description' => 'The translator field.',
            'type' => 'int',
          ),
          'location__country' => array(
            'description' => 'The location field.',
            'type' => 'varchar',
          ),
          'location__state' => array(
            'description' => 'The location field.',
            'type' => 'varchar',
          ),
          'location__city' => array(
            'description' => 'The location field.',
            'type' => 'varchar',
          ),
          'editor' => array(
            'description' => 'The editor field.',
            'type' => 'int',
          ),
          'editor_revision__target_id' => array(
            'description' => 'The editor_revision field.',
            'type' => 'int',
          ),
          'editor_revision__target_revision_id' => array(
            'description' => 'The editor_revision field.',
            'type' => 'int',
          ),
323 324 325 326
          'long_index_name' => array(
            'description' => 'The long_index_name field.',
            'type' => 'int',
          ),
327 328 329 330 331 332 333 334 335
          'default_langcode' => array(
            'description' => 'Boolean indicating whether field values are in the default entity language.',
            'type' => 'int',
            'size' => 'tiny',
            'not null' => TRUE,
            'default' => 1,
          ),
        ),
        'primary key' => array('id'),
336 337 338 339 340 341 342 343 344
        'unique keys' => array(
          'entity_test_field__uuid__value' => array('uuid'),
          'entity_test_field__hash__value' => array(array('hash', 10)),
          'entity_test_field__email__email' => array(
            'email__username',
            'email__hostname',
            array('email__domain', 3),
          ),
        ),
345 346 347 348 349 350 351 352 353 354
        'indexes' => array(
          'entity_test_field__owner__target_id' => array('owner'),
          'entity_test_field__translator__target_id' => array(
            array('translator', 10),
          ),
          'entity_test_field__location__country_state_city' => array(
            'location__country',
            'location__state',
            array('location__city', 10),
          ),
355 356 357 358
          'entity_test__b588603cb9' => array(
            array('long_index_name', 10),
          ),

359 360 361 362 363 364 365 366 367 368 369 370 371 372
        ),
        'foreign keys' => array(
          'entity_test_field__editor__user_id' => array(
            'table' => 'users',
            'columns' => array('editor' => 'uid'),
          ),
          'entity_test_field__editor_revision__user_id' => array(
            'table' => 'users',
            'columns' => array('editor_revision__target_id' => 'uid'),
          ),
        ),
      ),
    );

373
    $this->setUpStorageSchema($expected);
374 375 376 377 378 379 380 381 382

    $table_mapping = new DefaultTableMapping($this->storageDefinitions);
    $table_mapping->setFieldNames('entity_test', array_keys($this->storageDefinitions));
    $table_mapping->setExtraColumns('entity_test', array('default_langcode'));

    $this->storage->expects($this->any())
      ->method('getTableMapping')
      ->will($this->returnValue($table_mapping));

383 384 385
    $this->assertNull(
      $this->storageSchema->onEntityTypeCreate($this->entityType)
    );
386 387 388 389 390
  }

  /**
   * Tests the schema for revisionable, non-translatable entities.
   *
391 392 393 394 395 396 397 398
   * @covers ::__construct
   * @covers ::getEntitySchemaTables
   * @covers ::initializeBaseTable
   * @covers ::initializeRevisionTable
   * @covers ::addTableDefaults
   * @covers ::getEntityIndexName
   * @covers ::processRevisionTable
   * @covers ::processIdentifierSchema
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
   */
  public function testGetSchemaRevisionable() {
    $this->entityType = new ContentEntityType(array(
      'id' => 'entity_test',
      'entity_keys' => array(
        'id' => 'id',
        'revision' => 'revision_id',
      ),
    ));

    $this->storage->expects($this->exactly(2))
      ->method('getRevisionTable')
      ->will($this->returnValue('entity_test_revision'));

    $this->setUpStorageDefinition('revision_id', array(
      'columns' => array(
        'value' => array(
          'type' => 'int',
        ),
      ),
    ));

    $expected = array(
      'entity_test' => array(
        'description' => 'The base table for entity_test entities.',
        'fields' => array(
          'id' => array(
            'description' => 'The id field.',
            'type' => 'serial',
            'not null' => TRUE,
          ),
          'revision_id' => array(
            'description' => 'The revision_id field.',
            'type' => 'int',
          )
        ),
        'primary key' => array('id'),
436 437 438
        'unique keys' => array(
          'entity_test__revision_id' => array('revision_id'),
        ),
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
        'indexes' => array(),
        'foreign keys' => array(
          'entity_test__revision' => array(
            'table' => 'entity_test_revision',
            'columns' => array('revision_id' => 'revision_id'),
          )
        ),
      ),
      'entity_test_revision' => array(
        'description' => 'The revision table for entity_test entities.',
        'fields' => array(
          'id' => array(
            'description' => 'The id field.',
            'type' => 'int',
            'not null' => TRUE,
          ),
          'revision_id' => array(
            'description' => 'The revision_id field.',
            'type' => 'serial',
          ),
        ),
        'primary key' => array('revision_id'),
461
        'unique keys' => array(),
462 463 464 465 466 467 468 469 470 471 472 473
        'indexes' => array(
          'entity_test__id' => array('id'),
        ),
        'foreign keys' => array(
          'entity_test__revisioned' => array(
            'table' => 'entity_test',
            'columns' => array('id' => 'id'),
          ),
        ),
      ),
    );

474
    $this->setUpStorageSchema($expected);
475 476 477 478 479 480 481 482

    $table_mapping = new DefaultTableMapping($this->storageDefinitions);
    $table_mapping->setFieldNames('entity_test', array_keys($this->storageDefinitions));
    $table_mapping->setFieldNames('entity_test_revision', array_keys($this->storageDefinitions));

    $this->storage->expects($this->any())
      ->method('getTableMapping')
      ->will($this->returnValue($table_mapping));
483

484
    $this->storageSchema->onEntityTypeCreate($this->entityType);
485 486 487 488 489
  }

  /**
   * Tests the schema for non-revisionable, translatable entities.
   *
490 491 492 493 494 495
   * @covers ::__construct
   * @covers ::getEntitySchemaTables
   * @covers ::initializeDataTable
   * @covers ::addTableDefaults
   * @covers ::getEntityIndexName
   * @covers ::processDataTable
496 497 498 499 500 501
   */
  public function testGetSchemaTranslatable() {
    $this->entityType = new ContentEntityType(array(
      'id' => 'entity_test',
      'entity_keys' => array(
        'id' => 'id',
502
        'langcode' => 'langcode',
503 504 505
      ),
    ));

506
    $this->storage->expects($this->any())
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
      ->method('getDataTable')
      ->will($this->returnValue('entity_test_field_data'));

    $this->setUpStorageDefinition('langcode', array(
      'columns' => array(
        'value' => array(
          'type' => 'varchar',
        ),
      ),
    ));

    $expected = array(
      'entity_test' => array(
        'description' => 'The base table for entity_test entities.',
        'fields' => array(
          'id' => array(
            'description' => 'The id field.',
            'type' => 'serial',
            'not null' => TRUE,
          ),
          'langcode' => array(
            'description' => 'The langcode field.',
            'type' => 'varchar',
            'not null' => TRUE,
          )
        ),
        'primary key' => array('id'),
534
        'unique keys' => array(),
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
        'indexes' => array(),
        'foreign keys' => array(),
      ),
      'entity_test_field_data' => array(
        'description' => 'The data table for entity_test entities.',
        'fields' => array(
          'id' => array(
            'description' => 'The id field.',
            'type' => 'int',
            'not null' => TRUE,
          ),
          'langcode' => array(
            'description' => 'The langcode field.',
            'type' => 'varchar',
            'not null' => TRUE,
          ),
        ),
        'primary key' => array('id', 'langcode'),
553
        'unique keys' => array(),
554 555 556 557 558 559 560 561 562 563
        'indexes' => array(),
        'foreign keys' => array(
          'entity_test' => array(
            'table' => 'entity_test',
            'columns' => array('id' => 'id'),
          ),
        ),
      ),
    );

564
    $this->setUpStorageSchema($expected);
565

566 567 568 569 570 571 572 573
    $table_mapping = new DefaultTableMapping($this->storageDefinitions);
    $table_mapping->setFieldNames('entity_test', array_keys($this->storageDefinitions));
    $table_mapping->setFieldNames('entity_test_field_data', array_keys($this->storageDefinitions));

    $this->storage->expects($this->any())
      ->method('getTableMapping')
      ->will($this->returnValue($table_mapping));

574 575 576
    $this->assertNull(
      $this->storageSchema->onEntityTypeCreate($this->entityType)
    );
577 578 579 580 581
  }

  /**
   * Tests the schema for revisionable, translatable entities.
   *
582 583 584 585 586 587 588
   * @covers ::__construct
   * @covers ::getEntitySchemaTables
   * @covers ::initializeDataTable
   * @covers ::addTableDefaults
   * @covers ::getEntityIndexName
   * @covers ::initializeRevisionDataTable
   * @covers ::processRevisionDataTable
589 590 591 592 593 594 595
   */
  public function testGetSchemaRevisionableTranslatable() {
    $this->entityType = new ContentEntityType(array(
      'id' => 'entity_test',
      'entity_keys' => array(
        'id' => 'id',
        'revision' => 'revision_id',
596
        'langcode' => 'langcode',
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
      ),
    ));

    $this->storage->expects($this->exactly(3))
      ->method('getRevisionTable')
      ->will($this->returnValue('entity_test_revision'));
    $this->storage->expects($this->once())
      ->method('getDataTable')
      ->will($this->returnValue('entity_test_field_data'));
    $this->storage->expects($this->once())
      ->method('getRevisionDataTable')
      ->will($this->returnValue('entity_test_revision_field_data'));

    $this->setUpStorageDefinition('revision_id', array(
      'columns' => array(
        'value' => array(
          'type' => 'int',
        ),
      ),
    ));
    $this->setUpStorageDefinition('langcode', array(
      'columns' => array(
        'value' => array(
          'type' => 'varchar',
        ),
      ),
    ));

    $expected = array(
      'entity_test' => array(
        'description' => 'The base table for entity_test entities.',
        'fields' => array(
          'id' => array(
            'description' => 'The id field.',
            'type' => 'serial',
            'not null' => TRUE,
          ),
          'revision_id' => array(
            'description' => 'The revision_id field.',
            'type' => 'int',
          ),
          'langcode' => array(
            'description' => 'The langcode field.',
            'type' => 'varchar',
            'not null' => TRUE,
          )
        ),
        'primary key' => array('id'),
        'unique keys' => array(
          'entity_test__revision_id' => array('revision_id'),
        ),
648
        'indexes' => array(),
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
        'foreign keys' => array(
          'entity_test__revision' => array(
            'table' => 'entity_test_revision',
            'columns' => array('revision_id' => 'revision_id'),
          ),
        ),
      ),
      'entity_test_revision' => array(
        'description' => 'The revision table for entity_test entities.',
        'fields' => array(
          'id' => array(
            'description' => 'The id field.',
            'type' => 'int',
            'not null' => TRUE,
          ),
          'revision_id' => array(
            'description' => 'The revision_id field.',
            'type' => 'serial',
          ),
          'langcode' => array(
            'description' => 'The langcode field.',
            'type' => 'varchar',
            'not null' => TRUE,
          ),
        ),
        'primary key' => array('revision_id'),
675
        'unique keys' => array(),
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
        'indexes' => array(
          'entity_test__id' => array('id'),
        ),
        'foreign keys' => array(
          'entity_test__revisioned' => array(
            'table' => 'entity_test',
            'columns' => array('id' => 'id'),
          ),
        ),
      ),
      'entity_test_field_data' => array(
        'description' => 'The data table for entity_test entities.',
        'fields' => array(
          'id' => array(
            'description' => 'The id field.',
            'type' => 'int',
            'not null' => TRUE,
          ),
          'revision_id' => array(
            'description' => 'The revision_id field.',
            'type' => 'int',
          ),
          'langcode' => array(
            'description' => 'The langcode field.',
            'type' => 'varchar',
            'not null' => TRUE,
          ),
        ),
        'primary key' => array('id', 'langcode'),
705
        'unique keys' => array(),
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
        'indexes' => array(
          'entity_test__revision_id' => array('revision_id'),
        ),
        'foreign keys' => array(
          'entity_test' => array(
            'table' => 'entity_test',
            'columns' => array('id' => 'id'),
          ),
        ),
      ),
      'entity_test_revision_field_data' => array(
        'description' => 'The revision data table for entity_test entities.',
        'fields' => array(
          'id' => array(
            'description' => 'The id field.',
            'type' => 'int',
            'not null' => TRUE,
          ),
          'revision_id' => array(
            'description' => 'The revision_id field.',
            'type' => 'int',
          ),
          'langcode' => array(
            'description' => 'The langcode field.',
            'type' => 'varchar',
            'not null' => TRUE,
          ),
        ),
        'primary key' => array('revision_id', 'langcode'),
735
        'unique keys' => array(),
736 737 738 739 740 741 742 743 744 745 746 747 748 749
        'indexes' => array(),
        'foreign keys' => array(
          'entity_test' => array(
            'table' => 'entity_test',
            'columns' => array('id' => 'id'),
          ),
          'entity_test__revision' => array(
            'table' => 'entity_test_revision',
            'columns' => array('revision_id' => 'revision_id'),
          ),
        ),
      ),
    );

750
    $this->setUpStorageSchema($expected);
751

752 753 754 755 756 757 758 759 760 761
    $table_mapping = new DefaultTableMapping($this->storageDefinitions);
    $table_mapping->setFieldNames('entity_test', array_keys($this->storageDefinitions));
    $table_mapping->setFieldNames('entity_test_revision', array_keys($this->storageDefinitions));
    $table_mapping->setFieldNames('entity_test_field_data', array_keys($this->storageDefinitions));
    $table_mapping->setFieldNames('entity_test_revision_field_data', array_keys($this->storageDefinitions));

    $this->storage->expects($this->any())
      ->method('getTableMapping')
      ->will($this->returnValue($table_mapping));

762 763 764 765 766 767
    $this->storageSchema->onEntityTypeCreate($this->entityType);
  }

  /**
   * Tests the schema for a field dedicated table.
   *
768
   * @covers ::onFieldStorageDefinitionCreate
769 770
   * @covers ::getDedicatedTableSchema
   * @covers ::createDedicatedTableSchema
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
   */
  public function testDedicatedTableSchema() {
    $entity_type_id = 'entity_test';
    $this->entityType = new ContentEntityType(array(
      'id' => 'entity_test',
      'entity_keys' => array('id' => 'id'),
    ));

    // Setup a field having a dedicated schema.
    $field_name = $this->getRandomGenerator()->name();
    $this->setUpStorageDefinition($field_name, array(
      'columns' => array(
        'shape' => array(
          'type' => 'varchar',
          'length' => 32,
          'not null' => FALSE,
        ),
        'color' => array(
          'type' => 'varchar',
          'length' => 32,
          'not null' => FALSE,
        ),
      ),
      'foreign keys' => array(
        'color' => array(
          'table' => 'color',
          'columns' => array(
            'color' => 'id'
          ),
        ),
      ),
      'unique keys' => array(),
      'indexes' => array(),
    ));

    $field_storage = $this->storageDefinitions[$field_name];
    $field_storage
      ->expects($this->any())
      ->method('getType')
      ->will($this->returnValue('shape'));
    $field_storage
      ->expects($this->any())
      ->method('getTargetEntityTypeId')
      ->will($this->returnValue($entity_type_id));
    $field_storage
      ->expects($this->any())
      ->method('isMultiple')
      ->will($this->returnValue(TRUE));

    $this->storageDefinitions['id']
      ->expects($this->any())
      ->method('getType')
      ->will($this->returnValue('integer'));

    $expected = array(
      $entity_type_id . '__' . $field_name => array(
        'description' => "Data storage for $entity_type_id field $field_name.",
        'fields' => array(
          'bundle' => array(
            'type' => 'varchar',
            'length' => 128,
            'not null' => true,
            'default' => '',
            'description' => 'The field instance bundle to which this row belongs, used when deleting a field instance',
          ),
          'deleted' => array(
            'type' => 'int',
            'size' => 'tiny',
            'not null' => true,
            'default' => 0,
            'description' => 'A boolean indicating whether this data item has been deleted',
          ),
          'entity_id' => array(
            'type' => 'int',
            'unsigned' => true,
            'not null' => true,
            'description' => 'The entity id this data is attached to',
          ),
          'revision_id' => array(
            'type' => 'int',
            'unsigned' => true,
            'not null' => true,
            'description' => 'The entity revision id this data is attached to, which for an unversioned entity type is the same as the entity id',
          ),
          'langcode' => array(
            'type' => 'varchar',
            'length' => 32,
            'not null' => true,
            'default' => '',
            'description' => 'The language code for this data item.',
          ),
          'delta' => array(
            'type' => 'int',
            'unsigned' => true,
            'not null' => true,
            'description' => 'The sequence number for this data item, used for multi-value fields',
          ),
          $field_name . '_shape' => array(
            'type' => 'varchar',
            'length' => 32,
            'not null' => false,
          ),
          $field_name . '_color' => array(
            'type' => 'varchar',
            'length' => 32,
            'not null' => false,
          ),
        ),
        'primary key' => array('entity_id', 'deleted', 'delta', 'langcode'),
        'indexes' => array(
          'bundle' => array('bundle'),
          'deleted' => array('deleted'),
          'entity_id' => array('entity_id'),
          'revision_id' => array('revision_id'),
          'langcode' => array('langcode'),
        ),
        'foreign keys' => array(
          $field_name . '_color' => array(
            'table' => 'color',
            'columns' => array(
              $field_name . '_color' => 'id',
            ),
          ),
        ),
      ),
    );

    $this->setUpStorageSchema($expected);

    $table_mapping = new DefaultTableMapping($this->storageDefinitions);
    $table_mapping->setFieldNames($entity_type_id, array_keys($this->storageDefinitions));
    $table_mapping->setExtraColumns($entity_type_id, array('default_langcode'));

    $this->storage->expects($this->any())
      ->method('getTableMapping')
      ->will($this->returnValue($table_mapping));

908 909 910
    $this->assertNull(
      $this->storageSchema->onFieldStorageDefinitionCreate($field_storage)
    );
911 912 913
  }

  /**
914 915
   * Tests the schema for a field dedicated table for an entity with a string identifier.
   *
916
   * @covers ::onFieldStorageDefinitionCreate
917 918
   * @covers ::getDedicatedTableSchema
   * @covers ::createDedicatedTableSchema
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
   */
  public function testDedicatedTableSchemaForEntityWithStringIdentifier() {
    $entity_type_id = 'entity_test';
    $this->entityType = new ContentEntityType(array(
      'id' => 'entity_test',
      'entity_keys' => array('id' => 'id'),
    ));

    // Setup a field having a dedicated schema.
    $field_name = $this->getRandomGenerator()->name();
    $this->setUpStorageDefinition($field_name, array(
      'columns' => array(
        'shape' => array(
          'type' => 'varchar',
          'length' => 32,
          'not null' => FALSE,
        ),
        'color' => array(
          'type' => 'varchar',
          'length' => 32,
          'not null' => FALSE,
        ),
      ),
      'foreign keys' => array(
        'color' => array(
          'table' => 'color',
          'columns' => array(
            'color' => 'id'
          ),
        ),
      ),
      'unique keys' => array(),
      'indexes' => array(),
    ));

    $field_storage = $this->storageDefinitions[$field_name];
    $field_storage
      ->expects($this->any())
      ->method('getType')
      ->will($this->returnValue('shape'));
    $field_storage
      ->expects($this->any())
      ->method('getTargetEntityTypeId')
      ->will($this->returnValue($entity_type_id));
    $field_storage
      ->expects($this->any())
      ->method('isMultiple')
      ->will($this->returnValue(TRUE));

    $this->storageDefinitions['id']
      ->expects($this->any())
      ->method('getType')
      ->will($this->returnValue('string'));

    $expected = array(
      $entity_type_id . '__' . $field_name => array(
        'description' => "Data storage for $entity_type_id field $field_name.",
        'fields' => array(
          'bundle' => array(
            'type' => 'varchar',
            'length' => 128,
            'not null' => true,
            'default' => '',
            'description' => 'The field instance bundle to which this row belongs, used when deleting a field instance',
          ),
          'deleted' => array(
            'type' => 'int',
            'size' => 'tiny',
            'not null' => true,
            'default' => 0,
            'description' => 'A boolean indicating whether this data item has been deleted',
          ),
          'entity_id' => array(
            'type' => 'varchar',
            'length' => 128,
            'not null' => true,
            'description' => 'The entity id this data is attached to',
          ),
          'revision_id' => array(
            'type' => 'varchar',
            'length' => 128,
            'not null' => true,
            'description' => 'The entity revision id this data is attached to, which for an unversioned entity type is the same as the entity id',
          ),
          'langcode' => array(
            'type' => 'varchar',
            'length' => 32,
            'not null' => true,
            'default' => '',
            'description' => 'The language code for this data item.',
          ),
          'delta' => array(
            'type' => 'int',
            'unsigned' => true,
            'not null' => true,
            'description' => 'The sequence number for this data item, used for multi-value fields',
          ),
          $field_name . '_shape' => array(
            'type' => 'varchar',
            'length' => 32,
            'not null' => false,
          ),
          $field_name . '_color' => array(
            'type' => 'varchar',
            'length' => 32,
            'not null' => false,
          ),
        ),
        'primary key' => array('entity_id', 'deleted', 'delta', 'langcode'),
        'indexes' => array(
          'bundle' => array('bundle'),
          'deleted' => array('deleted'),
          'entity_id' => array('entity_id'),
          'revision_id' => array('revision_id'),
          'langcode' => array('langcode'),
        ),
        'foreign keys' => array(
          $field_name . '_color' => array(
            'table' => 'color',
            'columns' => array(
              $field_name . '_color' => 'id',
            ),
          ),
        ),
      ),
    );

    $this->setUpStorageSchema($expected);

    $table_mapping = new DefaultTableMapping($this->storageDefinitions);
    $table_mapping->setFieldNames($entity_type_id, array_keys($this->storageDefinitions));
    $table_mapping->setExtraColumns($entity_type_id, array('default_langcode'));

    $this->storage->expects($this->any())
      ->method('getTableMapping')
      ->will($this->returnValue($table_mapping));

1056 1057 1058
    $this->assertNull(
      $this->storageSchema->onFieldStorageDefinitionCreate($field_storage)
    );
1059 1060
  }

1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
  public function providerTestRequiresEntityDataMigration() {
    $updated_entity_type_definition = $this->getMockBuilder('\Drupal\Core\Entity\EntityTypeInterface')
      ->disableOriginalConstructor()
      ->getMock();
    $updated_entity_type_definition->expects($this->any())
      ->method('getStorageClass')
      // A class that exists, *any* class.
      ->willReturn('\Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema');
    $original_entity_type_definition = $this->getMockBuilder('\Drupal\Core\Entity\EntityTypeInterface')
      ->disableOriginalConstructor()
      ->getMock();
    $original_entity_type_definition->expects($this->any())
      ->method('getStorageClass')
      // A class that exists, *any* class.
      ->willReturn('\Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema');
    $original_entity_type_definition_other_nonexisting = $this->getMockBuilder('\Drupal\Core\Entity\EntityTypeInterface')
      ->disableOriginalConstructor()
      ->getMock();
    $original_entity_type_definition_other_nonexisting->expects($this->any())
      ->method('getStorageClass')
      ->willReturn('bar');
    $original_entity_type_definition_other_existing = $this->getMockBuilder('\Drupal\Core\Entity\EntityTypeInterface')
      ->disableOriginalConstructor()
      ->getMock();
    $original_entity_type_definition_other_existing->expects($this->any())
      ->method('getStorageClass')
      // A class that exists, *any* class.
      ->willReturn('\Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema');

    return [
      // Case 1: same storage class, ::hasData() === TRUE.
      [$updated_entity_type_definition, $original_entity_type_definition, TRUE, TRUE],
      // Case 2: same storage class, ::hasData() === FALSE.
      [$updated_entity_type_definition, $original_entity_type_definition, FALSE, FALSE],
      // Case 3: different storage class, original storage class does not exist.
      [$updated_entity_type_definition, $original_entity_type_definition_other_nonexisting, NULL, TRUE],
      // Case 4: different storage class, original storage class exists, ::hasData() === TRUE.
      [$updated_entity_type_definition, $original_entity_type_definition_other_existing, TRUE, TRUE],
      // Case 5: different storage class, original storage class exists, ::hasData() === FALSE.
      [$updated_entity_type_definition, $original_entity_type_definition_other_existing, FALSE, FALSE],
    ];
  }

  /**
   * @covers ::requiresEntityDataMigration
   *
   * @dataProvider providerTestRequiresEntityDataMigration
   */
  public function testRequiresEntityDataMigration($updated_entity_type_definition, $original_entity_type_definition, $original_storage_has_data, $migration_required) {
    $this->entityType = new ContentEntityType(array(
      'id' => 'entity_test',
      'entity_keys' => array('id' => 'id'),
    ));

    $original_storage = $this->getMockBuilder('Drupal\Core\Entity\Sql\SqlContentEntityStorage')
      ->disableOriginalConstructor()
      ->getMock();

    $original_storage->expects($this->exactly(is_null($original_storage_has_data) ? 0 : 1))
      ->method('hasData')
      ->willReturn($original_storage_has_data);

    // Assert hasData() is never called on the new storage definition.
    $this->storage->expects($this->never())
      ->method('hasData');

    $connection = $this->getMockBuilder('Drupal\Core\Database\Connection')
      ->disableOriginalConstructor()
      ->getMock();

    $this->entityManager->expects($this->any())
      ->method('createHandlerInstance')
      ->willReturn($original_storage);

    $this->storageSchema = $this->getMockBuilder('Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema')
      ->setConstructorArgs(array($this->entityManager, $this->entityType, $this->storage, $connection))
      ->setMethods(array('installedStorageSchema'))
      ->getMock();

    $this->assertEquals($migration_required, $this->storageSchema->requiresEntityDataMigration($updated_entity_type_definition, $original_entity_type_definition));
  }

1143 1144
  /**
   * Sets up the storage schema object to test.
1145
   *
1146 1147 1148 1149 1150
   * This uses the field definitions set in $this->storageDefinitions.
   *
   * @param array $expected
   *   (optional) An associative array describing the expected entity schema to
   *   be created. Defaults to expecting nothing.
1151
   */
1152
  protected function setUpStorageSchema(array $expected = array()) {
1153 1154 1155 1156 1157 1158
    $this->entityManager->expects($this->any())
      ->method('getDefinition')
      ->with($this->entityType->id())
      ->will($this->returnValue($this->entityType));

    $this->entityManager->expects($this->any())
1159 1160 1161
      ->method('getFieldStorageDefinitions')
      ->with($this->entityType->id())
      ->will($this->returnValue($this->storageDefinitions));
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193

    $db_schema_handler = $this->getMockBuilder('Drupal\Core\Database\Schema')
      ->disableOriginalConstructor()
      ->getMock();

    if ($expected) {
      $invocation_count = 0;
      $expected_table_names = array_keys($expected);
      $expected_table_schemas = array_values($expected);

      $db_schema_handler->expects($this->any())
        ->method('createTable')
        ->with(
          $this->callback(function($table_name) use (&$invocation_count, $expected_table_names) {
            return $expected_table_names[$invocation_count] == $table_name;
          }),
          $this->callback(function($table_schema) use (&$invocation_count, $expected_table_schemas) {
            return $expected_table_schemas[$invocation_count] == $table_schema;
          })
        )
        ->will($this->returnCallback(function() use (&$invocation_count) {
          $invocation_count++;
        }));
    }

    $connection = $this->getMockBuilder('Drupal\Core\Database\Connection')
      ->disableOriginalConstructor()
      ->getMock();
    $connection->expects($this->any())
      ->method('schema')
      ->will($this->returnValue($db_schema_handler));

1194 1195 1196 1197 1198 1199 1200 1201 1202
    $key_value = $this->getMock('Drupal\Core\KeyValueStore\KeyValueStoreInterface');
    $this->storageSchema = $this->getMockBuilder('Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema')
      ->setConstructorArgs(array($this->entityManager, $this->entityType, $this->storage, $connection))
      ->setMethods(array('installedStorageSchema'))
      ->getMock();
    $this->storageSchema
      ->expects($this->any())
      ->method('installedStorageSchema')
      ->will($this->returnValue($key_value));
1203 1204 1205 1206 1207 1208 1209 1210 1211
  }

  /**
   * Sets up a field definition.
   *
   * @param string $field_name
   *   The field name.
   * @param array $schema
   *   The schema array of the field definition, as returned from
1212
   *   FieldStorageDefinitionInterface::getSchema().
1213 1214
   */
  public function setUpStorageDefinition($field_name, array $schema) {
1215
    $this->storageDefinitions[$field_name] = $this->getMock('Drupal\Tests\Core\Field\TestBaseFieldDefinitionInterface');
1216 1217 1218 1219
    $this->storageDefinitions[$field_name]->expects($this->any())
      ->method('isBaseField')
      ->will($this->returnValue(TRUE));
    // getName() is called once for each table.
1220 1221 1222
    $this->storageDefinitions[$field_name]->expects($this->any())
      ->method('getName')
      ->will($this->returnValue($field_name));
1223 1224 1225 1226 1227 1228 1229 1230
    // getDescription() is called once for each table.
    $this->storageDefinitions[$field_name]->expects($this->any())
      ->method('getDescription')
      ->will($this->returnValue("The $field_name field."));
    // getSchema() is called once for each table.
    $this->storageDefinitions[$field_name]->expects($this->any())
      ->method('getSchema')
      ->will($this->returnValue($schema));
1231
    $this->storageDefinitions[$field_name]->expects($this->any())
1232 1233 1234 1235 1236
      ->method('getColumns')
      ->will($this->returnValue($schema['columns']));
  }

}