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

attempt to update Drupal 8 file object #27

Open
wants to merge 10 commits into
base: drupal8
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 @@ -9,6 +9,7 @@

use Behat\Gherkin\Node\TableNode;
use Palantirnet\PalantirBehatExtension\NotUpdatedException;
use Drupal\file\Entity\File;

/**
* Behat context class with additional file-related steps.
Expand All @@ -23,18 +24,16 @@ class DrupalFileContext extends SharedDrupalContext
* @Given the file :filename
*
* @param string $filename The name of a file within the MinkExtension's files_path directory.
* @param int $status 1 if the file is permanent and should not be deleted; 0 if the file is temporary. Defaults to 1.
* @param int $status FILE_STATUS_PERMANENT or 0 if the file is temporary. Defaults to FILE_STATUS_PERMANENT.
*
* @return void
*/
public function createFile($filename, $status = 1)
public function createFile($filename, $status = FILE_STATUS_PERMANENT)
{
throw new NotUpdatedException();

$file = (object) array(
'filename' => $filename,
'status' => $status,
);
$file = new File(array(), 'file');
$file->setFilename($filename);
$file->set('status', $status);

$file = $this->expandFile($file);

Expand All @@ -60,10 +59,14 @@ public function createFile($filename, $status = 1)
*/
public function createFiles(TableNode $filesTable)
{
throw new NotUpdatedException();

foreach ($filesTable->getHash() as $fileHash) {
$file = (object) $fileHash;
$file = new File(array(), 'file');
$file->setFilename($fileHash['filename']);

$status = isset($fileHash['status']) ?: FILE_STATUS_PERMANENT;
$file->set('status', $status);

$file = $this->expandFile($file);

$this->fileCreate($file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Drupal\DrupalExtension\Context\RawDrupalContext;
use Palantirnet\PalantirBehatExtension\NotUpdatedException;
use Drupal\file\Entity\File;

/**
* Behat context class with functionality that is shared across custom contexts.
Expand Down Expand Up @@ -164,17 +165,15 @@ public function findUserByName($userName)
*/
public function fileCreate($file)
{
throw new NotUpdatedException();

// Save the file and overwrite if it already exists.
$dest = file_build_uri(drupal_basename($file->uri));
$dest = file_build_uri(drupal_basename($file->GetFileUri()));
$result = file_copy($file, $dest, FILE_EXISTS_REPLACE);

// Stash the file object for later cleanup.
if (empty($result->fid) === false) {
if (empty($result->id()) === false) {
$this->files[] = $result;
} else {
throw new \Exception(sprintf('File "%s" could not be copied from "%s" to "%s".', $file->filename, $file->uri, $result->uri));
throw new \Exception(sprintf('File "%s" could not be copied from "%s" to "%s".', $file->getFilename(), $file->GetFileUri(), $result->GetFileUri()));
}

return $result;
Expand All @@ -194,25 +193,28 @@ public function fileCreate($file)
*/
public function expandFile($file)
{
throw new NotUpdatedException();

if (empty($file->filename) === true) {
if (empty($file->getFilename()) === true) {
throw new \Exception("Can't create file with no source filename; this should be the name of a file within the MinkExtension's files_path directory.");
}

// Set the URI to the path to the file within the MinkExtension's
// files_path parameter.
$file->uri = rtrim(realpath($this->getMinkParameter('files_path')), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$file->filename;
$file->setFileUri(rtrim(realpath($this->getMinkParameter('files_path')), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$file->getFilename());

$file->set('langcode', $file->language()->getId());

$file->setChangedTime(time());

// Assign authorship if none exists and `author` is passed.
if (isset($file->uid) === false && empty($file->author) === false) {
/*
if (isset($file->getOwnerId()) === false && empty($file->author) === false) {
$account = user_load_by_name($file->author);
if ($account !== false) {
$file->uid = $account->uid;
}
}

// Add default values.
Add default values.
$defaults = array(
'uid' => 0,
'status' => 1,
Expand All @@ -222,7 +224,7 @@ public function expandFile($file)
if (isset($file->$key) === false) {
$file->$key = $default;
}
}
} */

return $file;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
*/

namespace Palantirnet\PalantirBehatExtension;
use Behat\Behat\Context\Exception\ContextException;
use Symfony\Component\Config\Definition\Exception\InvalidDefinitionException;

/**
* Exception for when a method has not yet been updated for Drupal 8.
*/
class NotUpdatedException extends Exception
class NotUpdatedException extends InvalidDefinitionException implements ContextException
{
}//end class