Skip to content

Commit

Permalink
Merge pull request ezsystems#28 from miguelcleverti/basicContentContext
Browse files Browse the repository at this point in the history
QA-436 / QA-437 / QA-438 Basic content creation sentences
  • Loading branch information
andrerom committed Sep 1, 2015
2 parents 22d8a87 + b219385 commit d3e3780
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 0 deletions.
1 change: 1 addition & 0 deletions Context/EzContext.php
Expand Up @@ -24,6 +24,7 @@ class EzContext implements KernelAwareContext
use Object\UserGroup;
use Object\User;
use Object\FieldType;
use Object\BasicContent;

const DEFAULT_SITEACCESS_NAME = 'behat_site';

Expand Down
44 changes: 44 additions & 0 deletions Context/Object/BasicContent.php
@@ -0,0 +1,44 @@
<?php
/**
*This file is part of the BehatBundle package
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @version //autogentag//
*/

namespace EzSystems\BehatBundle\Context\Object;

use Behat\Gherkin\Node\TableNode;
use PHPUnit_Framework_Assert as Assertion;

/**
* Sentences for Fields
*/
trait BasicContent
{
/**
* @Given a/an :path folder exists
*/
public function createBasicFolder( $path )
{
$path = rtrim( $path, '/' );
$names = explode( '/', $path );
$name = end( $names );
$fields = array( 'name' => $name );
return $this->getBasicContentManager()->createContentwithPath( $path, $fields, 'folder' );
}

/**
* @Given a/an :path article exists
*/
public function createBasicArticle( $path )
{
$path = rtrim( $path, '/' );
$names = explode( '/', $path );
$title = end( $names );
$intro = '<?xml version="1.0" encoding="utf-8"?><section xmlns:custom="http://ez.no/namespaces/ezpublish3/custom/" xmlns:image="http://ez.no/namespaces/ezpublish3/image/" xmlns:xhtml="http://ez.no/namespaces/ezpublish3/xhtml/"><paragraph>This is a paragraph.</paragraph></section>';
$fields = array( 'title' => $title, 'intro' => $intro );
return $this->getBasicContentManager()->createContentwithPath( $path, $fields, 'article' );
}
}
116 changes: 116 additions & 0 deletions ObjectManager/BasicContent.php
@@ -0,0 +1,116 @@
<?php
/**
* This file is part of the BehatBundle package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @version //autogentag//
*/

namespace EzSystems\BehatBundle\ObjectManager;

use eZ\Publish\API\Repository\Values\ValueObject;
use eZ\Publish\API\Repository\Exceptions as ApiExceptions;
use Behat\Symfony2Extension\Context\KernelAwareContext;

class BasicContent extends Base
{
/**
* Default language
*/
const DEFAULT_LANGUAGE = 'eng-GB';
/**
* Content path mapping
*/
private $contentPaths = array();

/**
* Publishes the content
*
* @param string The field name
* @param mixed The field value
*/
public function createContent( $contentType, $fields, $location )
{
$repository = $this->getRepository();
$languageCode = self::DEFAULT_LANGUAGE;

$content = $repository->sudo(
function() use( $repository, $languageCode, $contentType, $fields, $location )
{
$contentService = $repository->getcontentService();
$contentTypeService = $repository->getContentTypeService();
$locationCreateStruct = $repository->getLocationService()->newLocationCreateStruct( $location );

$contentType = $contentTypeService->loadContentTypeByIdentifier( $contentType );
$contentCreateStruct = $contentService->newContentCreateStruct( $contentType, $languageCode );
foreach ( array_keys( $fields ) as $key )
{
$contentCreateStruct->setField( $key, $fields[$key] );
}
$draft = $contentService->createContent( $contentCreateStruct, array( $locationCreateStruct ) );
$content = $contentService->publishVersion( $draft->versionInfo );

return $content;
}
);

return $content->contentInfo->mainLocationId;
}

/**
* Creates and publishes a content in a given path
* the container are assumed to be folders
*
* @param string $path The content path
* @param mixed $contentType The content type identifier
*/
public function createContentWithPath( $path, $fields, $contentType )
{
$contentsName = explode( '/', $path );
$currentPath = '';
$location = '2';

foreach ( $contentsName as $name )
{
if ( $name != end( $contentsName ) )
{
$location = $this->createContent( 'folder', [ 'name' => $name ], $location );
}
if ( $currentPath != '' )
{
$currentPath .= '/';
}
$currentPath .= $name;
$this->mapContentPath( $currentPath );
}
$location = $this->createContent( $contentType, $fields, $location );

return $location;
}

/**
* Getter for $contentPaths
*/
public function getContentPath( $name )
{
return $this->contentPaths[$name];
}

/**
* Maps the path of the content to it's name for later use
*/
private function mapContentPath( $path )
{
$contentNames = explode( '/', $path );
$this->contentPaths[ end( $contentNames ) ] = $path;
}

/**
* NOT USED FOR NOW
*/
protected function destroy( ValueObject $object )
{
// do nothing for now, to be implemented later when decided waht to do with the created objects
}
}

0 comments on commit d3e3780

Please sign in to comment.