You are here

public function AssetTest::testAssetInstance in Mini site 8

Test working with Asset class instance.

File

tests/src/Functional/AssetTest.php, line 24

Class

AssetTest
Class AssetTest.

Namespace

Drupal\Tests\minisite\Functional

Code

public function testAssetInstance() {
  $asset = new Asset('node', $this->contentType, 1, Language::LANGCODE_DEFAULT, 'field_minisite_test', 'public://minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5/rootpath/subpath/page1.html');

  // Assert getters without alias set.
  $this
    ->assertEqual('public://minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5/rootpath/subpath/page1.html', $asset
    ->getUri());
  $this
    ->assertContains('minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5/rootpath/subpath/page1.html', $asset
    ->getUrl());

  // Assert getters with alias set.
  $asset
    ->setAliasPrefix('some/alias');
  $this
    ->assertEqual('public://minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5/rootpath/subpath/page1.html', $asset
    ->getUri());
  $this
    ->assertContains('/some/alias/rootpath/subpath/page1.html', $asset
    ->getUrl());
  $this
    ->assertNotContains('minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5', $asset
    ->getUrl());

  // Assert other getters.
  $this
    ->assertEquals(Language::LANGCODE_DEFAULT, $asset
    ->getLanguage());
  $this
    ->assertTrue($asset
    ->isDocument());
  $this
    ->assertFalse($asset
    ->isIndex());

  // Assert saving.
  $this
    ->assertNull($asset
    ->id());
  $asset
    ->save();
  $this
    ->assertNotNull($asset
    ->id());
  $previous_id = $asset
    ->id();
  $asset
    ->save();
  $this
    ->assertEquals($previous_id, $asset
    ->id(), 'Id has not changed after re-save');

  // Assert loading.
  $asset2 = Asset::load($previous_id);
  $this
    ->assertNotNull($asset2);
  $this
    ->assertEquals($previous_id, $asset2
    ->id());
  $asset3 = Asset::loadByAlias('/some/alias/rootpath/subpath/page1.html');
  $this
    ->assertNotNull($asset3);
  $this
    ->assertEquals($previous_id, $asset3
    ->id());
  $asset4 = Asset::loadByUri('public://minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5/rootpath/subpath/page1.html');
  $this
    ->assertNotNull($asset4);
  $this
    ->assertEquals($previous_id, $asset4
    ->id());

  // Deleting.
  try {
    $asset4
      ->delete();
  } catch (NotRegularFileException|NotRegularDirectoryException $exception) {

    // This test is not dealing with real files, so allow exceptions for
    // file removals.
  }
  $this
    ->assertNull($asset4
    ->id());
  $asset5 = Asset::load($previous_id);
  $this
    ->assertNull($asset5);
}