3
0
Fork 0
mirror of https://github.com/farmOS/farmOS.git synced 2024-02-23 11:37:38 +01:00
farmOS/modules/core/asset/tests/src/Functional/AssetCRUDTest.php
2020-10-01 15:51:51 -04:00

115 lines
2.9 KiB
PHP

<?php
namespace Drupal\Tests\asset\Functional;
use Drupal\asset\Entity\Asset;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* Tests the asset CRUD.
*
* @group farm
*/
class AssetCRUDTest extends AssetTestBase {
use StringTranslationTrait;
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Fields are displayed correctly.
*/
public function testFieldsVisibility() {
$this->drupalGet('asset/add/default');
$this->assertResponse('200');
$assert_session = $this->assertSession();
$assert_session->fieldExists('name[0][value]');
$assert_session->fieldExists('status');
$assert_session->fieldExists('revision_log_message[0][value]');
$assert_session->fieldExists('uid[0][target_id]');
$assert_session->fieldExists('created[0][value][date]');
$assert_session->fieldExists('created[0][value][time]');
}
/**
* Create asset entity.
*/
public function testCreateAsset() {
$assert_session = $this->assertSession();
$name = $this->randomMachineName();
$edit = [
'name[0][value]' => $name,
];
$this->drupalPostForm('asset/add/default', $edit, $this->t('Save'));
$result = \Drupal::entityTypeManager()
->getStorage('asset')
->getQuery()
->range(0, 1)
->execute();
$asset_id = reset($result);
$asset = Asset::load($asset_id);
$this->assertEquals($asset->get('name')->value, $name, 'asset has been saved.');
$assert_session->pageTextContains("Saved the $name asset.");
$assert_session->pageTextContains($name);
}
/**
* Display asset entity.
*/
public function testViewAsset() {
$edit = [
'name' => $this->randomMachineName(),
'created' => \Drupal::time()->getRequestTime(),
'done' => TRUE,
];
$asset = $this->createAssetEntity($edit);
$asset->save();
$this->drupalGet($asset->toUrl('canonical'));
$this->assertResponse(200);
$this->assertText($edit['name']);
$this->assertRaw(\Drupal::service('date.formatter')->format(\Drupal::time()->getRequestTime()));
}
/**
* Edit asset entity.
*/
public function testEditAsset() {
$asset = $this->createAssetEntity();
$asset->save();
$edit = [
'name[0][value]' => $this->randomMachineName(),
];
$this->drupalPostForm($asset->toUrl('edit-form'), $edit, $this->t('Save'));
$this->assertText($edit['name[0][value]']);
}
/**
* Delete asset entity.
*/
public function testDeleteAsset() {
$asset = $this->createAssetEntity();
$asset->save();
$label = $asset->getName();
$asset_id = $asset->id();
$this->drupalPostForm($asset->toUrl('delete-form'), [], $this->t('Delete'));
$this->assertRaw($this->t('The @entity-type %label has been deleted.', [
'@entity-type' => $asset->getEntityType()->getSingularLabel(),
'%label' => $label,
]));
$this->assertNull(Asset::load($asset_id));
}
}