From cca21ef77be007fdd4ff713bfd95034549417012 Mon Sep 17 00:00:00 2001 From: Bec White Date: Tue, 15 Dec 2015 15:08:31 -0600 Subject: [PATCH 1/3] Reference file objects by name. --- .../Context/EntityDataContext.php | 19 +++++++++++++ .../Context/SharedDrupalContext.php | 28 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php b/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php index 8778caa..1dddadd 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. * 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. * From 7c01a6a29680c8c1bf6e372aacba4b033b7f5fec Mon Sep 17 00:00:00 2001 From: Bec White Date: Tue, 15 Dec 2015 15:09:12 -0600 Subject: [PATCH 2/3] Verify the author/uid property of entities. --- .../Context/EntityDataContext.php | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php b/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php index 1dddadd..6bf1fb3 100644 --- a/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php +++ b/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php @@ -121,14 +121,45 @@ public function assertFileByName($fileName) */ 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. * From 8c920cebb866a5b09e9e230ce4e1111e9c6b8c8a Mon Sep 17 00:00:00 2001 From: Bec White Date: Tue, 15 Dec 2015 15:09:32 -0600 Subject: [PATCH 3/3] Verify the migration and source id of an entity. --- .../Context/EntityDataContext.php | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php b/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php index 6bf1fb3..0ef4848 100644 --- a/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php +++ b/src/Palantirnet/PalantirBehatExtension/Context/EntityDataContext.php @@ -477,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. *