Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] File entity data context #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,25 @@ public function assertUserByName($userName)
}//end assertUserByName()


/**
* Verify field and property values of a file entity.
*
* @When I examine the file :fileName
*
* @param string $fileName The name of a Drupal managed file.
*
* @return void
*/
public function assertFileByName($fileName)
{
$file = $this->findFileByName($fileName);

$this->currentEntity = $file;
$this->currentEntityType = 'file';

}//end assertFileByName()


/**
* Verify that an entity property is equal to a particular value.
*
Expand All @@ -102,14 +121,45 @@ public function assertUserByName($userName)
*/
public function assertEntityPropertyValue($property, $value)
{
$wrapper = entity_metadata_wrapper($this->currentEntityType, $this->currentEntity);
if ($wrapper->$property->value() !== $value) {
if (isset($this->currentEntity->$property) === false) {
throw new \Exception(sprintf('Entity has no property "%s"', $property));
}

if ((string) $this->currentEntity->$property !== $value) {
throw new \Exception(sprintf('Property "%s" is not "%s"', $property, $value));
}

}//end assertEntityPropertyValue()


/**
* Verify that an entity author has a particular username.
*
* @Then the entity author should be :userName
*
* @param string $userName A Drupal user name.
*
* @return void
*/
public function assertEntityAuthorName($userName)
{
$property = 'uid';
if (isset($this->currentEntity->$property) === false) {
throw new \Exception(sprintf('Entity has no property "%s"', $property));
}

$author = user_load($this->currentEntity->$property);
if (empty($author) === true) {
throw new \Exception(sprintf('Failed to load author user object id "%d".', $this->currentEntity->$property));
}

if ($author->name !== $userName) {
throw new \Exception(sprintf('Entity author name is "%s", not "%s".', $author->name, $userName));
}

}//end assertEntityAuthorName()


/**
* Verify that an entity property is not equal to a particular value.
*
Expand Down Expand Up @@ -427,6 +477,50 @@ public function assertEntityFieldValueDatetime($field, $value)
}//end assertEntityFieldValueDatetime()


/**
* Verify a migrated entity.
*
* @Then the entity has source id :sourceId from (the )migration :migrationName
*
* @param int $sourceId The source id of a migrated record.
* @param string $migrationName The name of a Drupal migration (not the class name).
*
* @return void
*/
public function assertEntityMigrationSourceId($sourceId, $migrationName)
{
if (module_exists('migrate') === false) {
throw new \Exception(sprintf('The "%s" module is not installed.', 'Migrate'));
}

$migration = \MigrationBase::getInstance($migrationName);
if (empty($migration) === true) {
throw new \Exception(sprintf('Could not find a migration named "%s".', $migrationName));
}

$destination = $migration->getDestination();
if (is_a($destination, 'MigrateDestinationEntity') === false) {
throw new \Exception(sprintf('Migration "%s" does not create entities.'));
}

if ($destination->getEntityType() !== $this->currentEntityType) {
throw new \Exception(sprintf('Migration "%s" does not create "%s" entities.'));
}

list($entityId, $revisionId, $bundle) = entity_extract_ids($this->currentEntityType, $this->currentEntity);
if ($destination->getBundle() !== $bundle) {
throw new \Exception(sprintf('Migration "%s" does not create "%s" entities of type "%s".', $migrationName, $bundle, $this->currentEntityType));
}

$map = $migration->getMap();
$row = $map->getRowByDestination(array($entityId));
if ($row['sourceid1'] !== $sourceId) {
throw new \Exception(sprintf('This entity does not have source id "%s" from migration "%s".', $sourceId, $migrationName));
}

}//end assertEntityMigrationSourceId()


/**
* Output the contents of a field on the current entity.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,34 @@ public function findUserByName($userName)
}//end findUserByName()


/**
* Get a file object by name.
*
* @param string $fileName The name of a Drupal managed file.
*
* @return stdclass
* The Drupal file object, if it exists.
*/
public function findFileByName($fileName)
{
$query = new \EntityFieldQuery();

$entities = $query->entityCondition('entity_type', 'file')
->propertyCondition('filename', $fileName)
->execute();

if (empty($entities['file']) === false && count($entities['file']) === 1) {
$id = key($entities['file']);
return file_load($id);
} else if (empty($entities['file']) === false && count($entities['file']) > 1) {
throw new \Exception(sprintf('Found more than one file named "%s"', $fileName));
} else {
throw new \Exception(sprintf('No file named "%s" exists', $fileName));
}

}//end findFileByName()


/**
* Save a file.
*
Expand Down