View source
<?php
namespace Drupal\Tests\minisite\Functional;
use Drupal\Component\Utility\Random;
use Drupal\Core\File\Exception\NotRegularDirectoryException;
use Drupal\Core\File\Exception\NotRegularFileException;
use Drupal\Core\Language\Language;
use Drupal\minisite\Asset;
use Drupal\minisite\Exception\AssetException;
class AssetTest extends MinisiteTestBase {
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');
$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());
$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());
$this
->assertEquals(Language::LANGCODE_DEFAULT, $asset
->getLanguage());
$this
->assertTrue($asset
->isDocument());
$this
->assertFalse($asset
->isIndex());
$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');
$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());
try {
$asset4
->delete();
} catch (NotRegularFileException|NotRegularDirectoryException $exception) {
}
$this
->assertNull($asset4
->id());
$asset5 = Asset::load($previous_id);
$this
->assertNull($asset5);
}
public function testAssetFromValues($values, $expect_exception) {
if ($expect_exception) {
$this
->expectException(AssetException::class);
}
$instance = Asset::fromValues($values);
$this
->assertNotNull($instance);
}
public function dataProviderAssetFromValues() {
return [
[
[
'entity_type' => 'someval',
'entity_bundle' => 'someval',
'entity_id' => 'someval',
'entity_language' => 'someval',
'field_name' => 'someval',
'source' => 'someval',
],
FALSE,
],
[
[
'entity_type' => 'someval',
'entity_bundle' => 'someval',
'entity_id' => 'someval',
'entity_language' => 'someval',
'field_name' => 'someval',
'source' => 'someval',
],
FALSE,
],
[
[
'entity_type' => 'someval',
'entity_bundle' => 'someval',
'entity_id' => 'someval',
'entity_language' => 'someval',
'field_name' => 'someval',
],
TRUE,
],
[
[
'entity_type' => '',
'entity_bundle' => 'someval',
'entity_id' => NULL,
'entity_language' => 'someval',
'field_name' => FALSE,
'source' => 'someval',
],
TRUE,
],
];
}
public function testIsIndex($path, $is_index) {
$instance = new Asset('node', $this->contentType, 1, Language::LANGCODE_DEFAULT, 'field_minisite_test', $path);
$this
->assertEqual($instance
->isIndex(), $is_index);
}
public function dataProviderIsIndex() {
return [
[
'public://minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5/rootpath/index.html',
TRUE,
],
[
'public://minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5/rootpath/page.html',
FALSE,
],
[
'public://minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5/rootpath/subpath/index.html',
FALSE,
],
[
'public://minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5/rootpath/subpath/page.html',
FALSE,
],
];
}
public function testSaveLong() {
$randomizer = new Random();
$prefix = 'public://minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5/';
$suffix = '.html';
$dir_path = $randomizer
->name(10) . '/';
$file_path = $randomizer
->name(2048 - strlen($dir_path) - strlen($prefix) - strlen($suffix)) . $suffix;
$path = $prefix . $dir_path . $file_path;
$asset = new Asset('node', $this->contentType, 1, Language::LANGCODE_DEFAULT, 'field_minisite_test', $path);
$this
->assertNull($asset
->id(), 'Unsaved asset does not have and id');
$asset
->save();
$this
->assertNotNull($asset
->id(), 'Saved asset has an id');
$loaded_asset = Asset::load($asset
->id());
$this
->assertNotNull($loaded_asset, 'Previously saved asset is not null');
$alias_prefix = '/' . $randomizer
->name(2048 - strlen($file_path) - 2);
$full_alias = $alias_prefix . '/' . $dir_path . $file_path;
$asset
->setAliasPrefix($alias_prefix);
$this
->assertEqual($asset
->getUrl(), $full_alias);
$asset
->save();
$asset_loaded_by_alias = Asset::loadByAlias($full_alias);
$this
->assertNotNull($asset_loaded_by_alias, 'Re-saved asset with an alias is not null');
$this
->assertNotNull($asset_loaded_by_alias
->id(), 'Re-saved asset with an alias has an id');
}
public function testLoadAll() {
$asset1 = new Asset('node', $this->contentType, 1, Language::LANGCODE_DEFAULT, 'field_minisite_test', $this
->getStubAssetPath());
$asset1
->save();
$asset2 = new Asset('node', $this->contentType, 1, Language::LANGCODE_DEFAULT, 'field_minisite_test', $this
->getStubAssetPath());
$asset2
->save();
$asset3 = new Asset('node', $this->contentType, 1, Language::LANGCODE_DEFAULT, 'field_minisite_test', $this
->getStubAssetPath());
$asset3
->save();
$loaded = Asset::loadAll();
$this
->assertEquals(3, count($loaded));
$this
->assertEquals($asset3
->id(), $loaded[0]
->id());
$this
->assertEquals($asset2
->id(), $loaded[1]
->id());
$this
->assertEquals($asset1
->id(), $loaded[2]
->id());
}
}