Skip to content

Commit

Permalink
Merge pull request #34 from ezsystems/ezp24869-viewContent_deprecation
Browse files Browse the repository at this point in the history
ObjectManager/BasicContent draft support
  • Loading branch information
bdunogier committed Oct 5, 2015
2 parents 1f4d45d + d10fead commit bebbb1b
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 39 deletions.
42 changes: 29 additions & 13 deletions Context/Browser/SubContext/CommonActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

/**
* Class with the simple actions you can do in a browser
*
* @method \Behat\Mink\Session getSession
*/
trait CommonActions
{
Expand Down Expand Up @@ -68,20 +70,33 @@ public function iCanSee( $id )
public function iAmOnPage( $page = 'home' )
{
$this->visit( $this->getPathByPageIdentifier( $page ) );
try
{
$statusCode = $this->getSession()->getStatusCode();
$valid = false;
if ( $statusCode >= 200 && $statusCode < 400 )
{
$valid = true;
$this->checkForExceptions();
}

/**
* Checks the output of the latest session page for Symfony exceptions.
*
* If one is found, a failed assertion is executed, with the exception details + formatted stacktrace.
*/
protected function checkForExceptions()
{
$exceptionElements = $this->getXpath()->findXpath("//div[@class='text-exception']/h1");
$exceptionStackTraceItems = $this->getXpath()->findXpath("//ol[@id='traces-0']/li");
if (count($exceptionElements) > 0) {
$exceptionElement = $exceptionElements[0];
$exceptionLines = [$exceptionElement->getText(), ''];

foreach ($exceptionStackTraceItems as $stackTraceItem) {
$html = $stackTraceItem->getHtml();
$html = substr($html, 0, strpos($html, '<a href', 1));
$html = htmlspecialchars_decode(strip_tags($html));
$html = preg_replace('/\s+/', ' ', $html);
$html = str_replace(' (', '(', $html);
$html = str_replace(' ->', '->', $html);
$exceptionLines[] = trim($html);
}
Assertion::assertTrue( $valid, "Invalid response status code '{$statusCode}'" );
}
// several mink drivers do not support getStatusCode()
catch ( UnsupportedDriverActionException $e )
{
// do nothing
$message = 'An exception occured during rendering:' . implode("\n", $exceptionLines);
Assertion::assertTrue(false, $message);
}
}

Expand Down Expand Up @@ -517,6 +532,7 @@ public function onPageSectionISeeText( $text, $pageSection = null )
*/
public function iSeeMessage( $text )
{
$this->checkForExceptions();
$this->assertSession()->pageTextContains( $text );
}

Expand Down
42 changes: 33 additions & 9 deletions Context/Object/BasicContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

/**
* Sentences for Fields
*
* @method \EzSystems\BehatBundle\ObjectManager\BasicContent getBasicContentManager
*/
trait BasicContent
{
Expand All @@ -22,10 +24,7 @@ trait BasicContent
*/
public function createBasicFolder( $path )
{
$path = rtrim( $path, '/' );
$names = explode( '/', $path );
$name = end( $names );
$fields = array( 'name' => $name );
$fields = array( 'name' => $this->getTitleFromPath( $path ) );
return $this->getBasicContentManager()->createContentwithPath( $path, $fields, 'folder' );
}

Expand All @@ -34,11 +33,36 @@ public function createBasicFolder( $path )
*/
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 );
$fields = array(
'title' => $this->getTitleFromPath( $path ),
'intro' => $this->getDummyXmlText()
);
return $this->getBasicContentManager()->createContentwithPath( $path, $fields, 'article' );
}

/**
* @Given a/an :path article draft exists
*/
public function createArticleDraft( $path )
{
$fields = array(
'title' => $this->getTitleFromPath( $path ),
'intro' => $this->getDummyXmlText()
);
return $this->getBasicContentManager()->createContentDraft( 2, 'article', $fields );
}

private function getTitleFromPath( $path )
{
$parts = explode( '/', rtrim( $path, '/' ) );
return end( $parts );
}

/**
* @return string
*/
private function getDummyXmlText()
{
return '<?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>';
}
}
77 changes: 60 additions & 17 deletions ObjectManager/BasicContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

namespace EzSystems\BehatBundle\ObjectManager;

use eZ\Publish\API\Repository\Repository;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\API\Repository\Values\ValueObject;
use eZ\Publish\API\Repository\Exceptions as ApiExceptions;
use Behat\Symfony2Extension\Context\KernelAwareContext;
Expand All @@ -19,52 +22,92 @@ class BasicContent extends Base
* Default language
*/
const DEFAULT_LANGUAGE = 'eng-GB';

/**
* Content path mapping
*/
private $contentPaths = array();

/**
* Publishes the content
* Creates and publishes a Content.
*
* @param string $contentType
* @param array $fields
* @param mixxed $parentLocationId
*
* @param string The field name
* @param mixed The field value
* @return mixed The content's main location id
*/
public function createContent( $contentType, $fields, $location )
public function createContent( $contentType, $fields, $parentLocationId )
{
$repository = $this->getRepository();
$languageCode = self::DEFAULT_LANGUAGE;

$content = $this->createContentDraft($parentLocationId, $contentType, $languageCode, $fields);
$content = $repository->getContentService()->publishVersion($content->versionInfo);

return $content->contentInfo->mainLocationId;
}

/**
* Publishes a content draft.
*
* @param Content $content
*/
public function publishDraft( Content $content )
{
$this->getRepository()->sudo(
function(Repository $repository) use ( $content )
{
$repository->getContentService()->publishVersion($content->versionInfo->id);
}
);
}

/**
* Creates a content draft using sudo().
*
* @param Location $parentLocationId
* @param string $contentTypeIdentifier
* @param string $languageCode
* @param array $fields Fields, as primitives understood by setField
*
*@return Content an unpublished Content draft
*/
function createContentDraft($parentLocationId, $contentTypeIdentifier, $fields, $languageCode = null )
{
$languageCode = $languageCode ?: self::DEFAULT_LANGUAGE;

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

$contentType = $contentTypeService->loadContentTypeByIdentifier( $contentType );
$contentCreateStruct = $contentService->newContentCreateStruct( $contentType, $languageCode );
foreach ( array_keys( $fields ) as $key )
{
$contentTypeIdentifier = $contentTypeService->loadContentTypeByIdentifier( $contentTypeIdentifier );
$contentCreateStruct = $contentService->newContentCreateStruct( $contentTypeIdentifier, $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 $contentService->createContent( $contentCreateStruct, array( $locationCreateStruct ) );
}
);

$this->addObjectToList( $content );
return $content->contentInfo->mainLocationId;

return $content;
}

/**
* Creates and publishes a content in a given path
* the container are assumed to be folders
* Creates and publishes a content at a given path.
* Non-existing path items are created as folders named after the path element.
*
* @param string $path The content path
* @param array $fields
* @param mixed $contentType The content type identifier
*
* @return mixed|string
*/
public function createContentWithPath( $path, $fields, $contentType )
{
Expand Down

0 comments on commit bebbb1b

Please # to comment.