You are here

protected function FractionUpdateTest::setUp in Fraction 8

Same name and namespace in other branches
  1. 2.x tests/src/Kernel/FractionUpdateTest.php \Drupal\Tests\fraction\Kernel\FractionUpdateTest::setUp()

Overrides EntityKernelTestBase::setUp

File

tests/src/Kernel/FractionUpdateTest.php, line 53

Class

FractionUpdateTest
Provides tests for updating the schema from signed to unsigned.

Namespace

Drupal\Tests\fraction\Kernel

Code

protected function setUp() {
  parent::setUp();
  $this->connection = Database::getConnection();
  $this->schema = $this->connection
    ->schema();
  $node_type = NodeType::create([
    'type' => 'article',
    'name' => 'Article',
  ]);
  $node_type
    ->save();
  FieldStorageConfig::create([
    'field_name' => 'field_fraction_node',
    'entity_type' => 'node',
    'type' => 'fraction',
    'cardinality' => -1,
  ])
    ->save();
  FieldConfig::create([
    'field_name' => 'field_fraction_node',
    'entity_type' => 'node',
    'bundle' => 'article',
    'label' => $this
      ->randomMachineName() . '_label',
  ])
    ->save();
  FieldStorageConfig::create([
    'field_name' => 'field_fraction_user',
    'entity_type' => 'user',
    'type' => 'fraction',
    'cardinality' => -1,
  ])
    ->save();
  FieldConfig::create([
    'field_name' => 'field_fraction_user',
    'entity_type' => 'user',
    'bundle' => 'user',
    'label' => $this
      ->randomMachineName() . '_label',
  ])
    ->save();
  $this->fieldsToUpdate = [
    [
      'table_name' => 'node__field_fraction_node',
      'columns' => [
        'numerator' => 'field_fraction_node_numerator',
        'denominator' => 'field_fraction_node_denominator',
      ],
    ],
    [
      'table_name' => 'user__field_fraction_user',
      'columns' => [
        'numerator' => 'field_fraction_user_numerator',
        'denominator' => 'field_fraction_user_denominator',
      ],
    ],
  ];

  // Revert all the fields (with no data) to be unsigned so the post_update
  // hook can be run.
  foreach ($this->fieldsToUpdate as $field) {
    $this->schema
      ->changeField($field['table_name'], $field['columns']['denominator'], $field['columns']['denominator'], [
      'type' => 'int',
      'not null' => TRUE,
      'unsigned' => TRUE,
      'default' => 1,
    ]);
  }
}