You are here

public function SqlContentEntityStorageTest::testCleanIds in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageTest.php \Drupal\Tests\Core\Entity\Sql\SqlContentEntityStorageTest::testCleanIds()

Tests entity ID sanitization.

File

core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageTest.php, line 1387
Contains \Drupal\Tests\Core\Entity\Sql\SqlContentEntityStorageTest.

Class

SqlContentEntityStorageTest
@coversDefaultClass \Drupal\Core\Entity\Sql\SqlContentEntityStorage @group Entity

Namespace

Drupal\Tests\Core\Entity\Sql

Code

public function testCleanIds() {
  $valid_ids = [
    -1,
    0,
    1,
    '-1',
    '0',
    '1',
    0123,
    -0x1a,
    0x1afc,
    -0b111,
    0b101,
    '0123',
    '00123',
    '000123',
    '-0123',
    '-00123',
    '-000123',
    -10.0,
    -1.0,
    0.0,
    1.0,
    10.0,
    -10.0,
    -1.0,
    0.0,
    1.0,
    10.0,
  ];
  $this->fieldDefinitions = $this
    ->mockFieldDefinitions([
    'id',
  ]);
  $this->fieldDefinitions['id']
    ->expects($this
    ->any())
    ->method('getType')
    ->will($this
    ->returnValue('integer'));
  $this
    ->setUpEntityStorage();
  $this->entityType
    ->expects($this
    ->any())
    ->method('getKey')
    ->willReturnMap([
    [
      'id',
      'id',
    ],
  ]);
  $method = new \ReflectionMethod($this->entityStorage, 'cleanIds');
  $method
    ->setAccessible(TRUE);
  $this
    ->assertEquals($valid_ids, $method
    ->invoke($this->entityStorage, $valid_ids));
  $invalid_ids = [
    '--1',
    '-0x1A',
    '0x1AFC',
    '-0b111',
    '0b101',
    'a',
    FALSE,
    TRUE,
    NULL,
    '32acb',
    123.123,
    123.678,
  ];
  $this
    ->assertEquals([], $method
    ->invoke($this->entityStorage, $invalid_ids));
}