diff --git a/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php b/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php index 8778caa..0ef4848 100644 --- a/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php +++ b/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php @@ -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. * @@ -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. * @@ -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. * diff --git a/src/Palantirnet/PalantirBehatExtension/Context/SharedDrupalContext.php b/src/Palantirnet/PalantirBehatExtension/Context/SharedDrupalContext.php index f502b35..3ab981e 100644 --- a/src/Palantirnet/PalantirBehatExtension/Context/SharedDrupalContext.php +++ b/src/Palantirnet/PalantirBehatExtension/Context/SharedDrupalContext.php @@ -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. *