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

[WIP] Add image tests #47

Merged
merged 1 commit into from
Sep 4, 2013
Merged
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
8 changes: 4 additions & 4 deletions Tests/Resources/Controller/PhpcrFileTestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function getUploadForm()
;
}

public function fileAction(Request $request)
public function indexAction(Request $request)
{
$fileClass = 'Symfony\Cmf\Bundle\MediaBundle\Doctrine\Phpcr\File';
$dm = $this->get('doctrine_phpcr')->getManager('default');
Expand All @@ -26,9 +26,9 @@ public function fileAction(Request $request)
$editorUploadForm = $this->getUploadForm();

return $this->render('::tests/file.html.twig', array(
'upload_form' => $uploadForm->createView(),
'editor_form' => $editorUploadForm->createView(),
'files' => $files,
'upload_form' => $uploadForm->createView(),
'editor_form' => $editorUploadForm->createView(),
'files' => $files,
));
}

Expand Down
172 changes: 172 additions & 0 deletions Tests/Resources/Controller/PhpcrImageTestController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?php

namespace Symfony\Cmf\Bundle\MediaBundle\Tests\Resources\Controller;

use Doctrine\ODM\PHPCR\Document\Generic;
use PHPCR\Util\PathHelper;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Cmf\Bundle\MediaBundle\Tests\Resources\Document\Content;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class PhpcrImageTestController extends Controller
{
protected function getUploadForm($type = null)
{
return $this->container->get('form.factory')->createNamedBuilder(null, 'form')
->add('image', 'file')
->getForm()
;
}

protected function getContentForm(Content $contentObject = null, array $imageOptions = array())
{
if (is_null($contentObject)) {
$contentObject = new Content();
}

return $this->createFormBuilder($contentObject)
->add('name')
->add('title')
->add('image', 'cmf_media_image', array_merge(array('required' => false), $imageOptions))
->getForm()
;
}

protected function getUrlSafePath($object)
{
return ltrim($object->getId(), '/');
}

protected function mapUrlSafePathToId($path)
{
// The path is being the id
return PathHelper::absolutizePath($path, '/');
}

public function indexAction(Request $request)
{
$dm = $this->get('doctrine_phpcr')->getManager('default');

// get image(s)
$imageClass = 'Symfony\Cmf\Bundle\MediaBundle\Doctrine\Phpcr\Image';
$images = $dm->getRepository($imageClass)->findAll();

// get content with image object
$contentClass = 'Symfony\Cmf\Bundle\MediaBundle\Tests\Resources\Document\Content';
$contentObject = $dm->getRepository($contentClass)->findOneBy(array());

$uploadForm = $this->getUploadForm();
$editorUploadForm = $this->getUploadForm();

// Form - content object with image embedded
$newContentForm = $this->getContentForm(null, array('required' => true));
$contentForm = $this->getContentForm($contentObject, array('imagine_filter' => false));
$contentFormImagine = $this->getContentForm($contentObject);

// action url
if ($contentObject) {
$contentFormEditAction = $this->generateUrl('phpcr_image_test_content_edit', array(
'path' => $this->getUrlSafePath($contentObject),
));
} else {
$contentFormEditAction = false;
}

return $this->render('::tests/image.html.twig', array(
'upload_form' => $uploadForm->createView(),
'editor_form' => $editorUploadForm->createView(),
'content_form_new' => $newContentForm->createView(),
'content_form' => $contentForm->createView(),
'content_form_imagine' => $contentFormImagine->createView(),
'content_form_edit_action' => $contentFormEditAction,
'images' => $images,
));
}

public function uploadAction(Request $request)
{
$form = $this->getUploadForm();

if ($request->isMethod('POST')) {
$form->bind($request);

if ($form->isValid()) {
/** @var UploadFileHelper $uploadFileHelper */
$uploadImageHelper = $this->get('cmf_media.upload_image_helper');

$uploadedFile = $request->files->get('image');

$image = $uploadImageHelper->handleUploadedFile($uploadedFile);

// persist
$dm = $this->get('doctrine_phpcr')->getManager('default');
$dm->persist($image);
$dm->flush();
}
}

return $this->redirect($this->generateUrl('phpcr_image_test'));
}

public function newAction(Request $request)
{
$dm = $this->get('doctrine_phpcr')->getManager('default');
$contentRoot = $dm->find(null, '/test/content');

if (!$contentRoot) {
$root = $dm->find(null, '/test');
$contentRoot = new Generic();
$contentRoot->setNodename('content');
$contentRoot->setParent($root);
$dm->persist($contentRoot);
}

$contentObject = new Content();
$contentObject->setParent($contentRoot);

$form = $this->getContentForm($contentObject);

if ($request->isMethod('POST')) {
$form->bind($request);

if ($form->isValid()) {
// persist
$dm = $this->get('doctrine_phpcr')->getManager('default');
$dm->persist($contentObject);
$dm->flush();
}
}

return $this->redirect($this->generateUrl('phpcr_image_test'));
}

public function editAction(Request $request, $path)
{
$dm = $this->get('doctrine_phpcr')->getManager('default');

$contentObject = $dm->find(null, $this->mapUrlSafePathToId($path));

if (!$contentObject || !$contentObject instanceof Content) {
throw new NotFoundHttpException(sprintf(
'Object with identifier %s cannot be resolved to a valid instance of Symfony\Cmf\Bundle\MediaBundle\Tests\Resources\Document\Content',
$path
));
}

$form = $this->getContentForm($contentObject);

if ($request->isMethod('POST')) {
$form->bind($request);

if ($form->isValid()) {
// persist
$dm = $this->get('doctrine_phpcr')->getManager('default');
$dm->persist($contentObject);
$dm->flush();
}
}

return $this->redirect($this->generateUrl('phpcr_image_test'));
}
}
34 changes: 33 additions & 1 deletion Tests/Resources/DataFixtures/Phpcr/LoadMediaData.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ODM\PHPCR\Document\Generic;
use Symfony\Cmf\Bundle\MediaBundle\Doctrine\Phpcr\File;
use Symfony\Cmf\Bundle\MediaBundle\Doctrine\Phpcr\Image;
use Symfony\Cmf\Bundle\MediaBundle\Tests\Resources\Document\Content;

class LoadMediaData implements FixtureInterface, DependentFixtureInterface
{
Expand All @@ -19,12 +21,22 @@ public function getDependencies()

public function load(ObjectManager $manager)
{
$testDataDir = realpath(__DIR__ . '/../../app/Resources/data');

$root = $manager->find(null, '/test');
$mediaRoot = new Generic;

// media root
$mediaRoot = new Generic();
$mediaRoot->setNodename('media');
$mediaRoot->setParent($root);
$manager->persist($mediaRoot);

// content root
$contentRoot = new Generic();
$contentRoot->setNodename('content');
$contentRoot->setParent($root);
$manager->persist($contentRoot);

// File
$file = new File();
$file->setParent($mediaRoot);
Expand All @@ -33,6 +45,26 @@ public function load(ObjectManager $manager)
$file->setContentType('text/plain');
$manager->persist($file);

// Image
$image = new Image();
$image->setParent($mediaRoot);
$image->setName('cmf-logo.png');
$image->setFileContentFromFilesystem($testDataDir .'/cmf-logo.png');
$manager->persist($image);

// Content
$content = new Content();
$content->setParent($contentRoot);
$content->setName('content-with-image');
$content->setTitle('Content document with image embedded');

$contentImage = new Image();
$contentImage->setName('cmf-logo.png');
$contentImage->setFileContentFromFilesystem($testDataDir .'/cmf-logo.png');

$content->setImage($contentImage);
$manager->persist($content);

$manager->flush();
}
}
69 changes: 69 additions & 0 deletions Tests/Resources/Document/Content.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Symfony\Cmf\Bundle\MediaBundle\Tests\Resources\Document;

use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRODM;
use Symfony\Cmf\Bundle\MediaBundle\Doctrine\Phpcr\Image;
use Symfony\Cmf\Bundle\MediaBundle\ImageInterface;
use Symfony\Cmf\Component\Testing\Document\Content as BaseContent;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
* @PHPCRODM\Document(referenceable=true)
*/
class Content extends BaseContent
{
/**
* @PHPCRODM\Child(cascade="persist")
*/
protected $image;

/**
* Set the image for this block.
*
* Setting null will do nothing, as this is what happens when you edit this
* block in a form without uploading a replacement file.
*
* If you need to delete the Image, you can use getImage and delete it with
* the document manager. Note that this block does not make much sense
* without an image, though.
*
* @param ImageInterface|UploadedFile|null $image optional the image to update
*/
public function setImage($image = null)
{
if (!$image) {
return;
}

if (!$image instanceof ImageInterface && !$image instanceof UploadedFile) {
$type = is_object($image) ? get_class($image) : gettype($image);

throw new \InvalidArgumentException(sprintf(
'Image is not a valid type, "%s" given.',
$type
));
}

if ($this->image) {
// existing image, only update content
// TODO: https://github.com/doctrine/phpcr-odm/pull/262
$this->image->copyContentFromFile($image);
} elseif ($image instanceof ImageInterface) {
$this->image = $image;
} else {
$this->image = new Image();
$this->image->copyContentFromFile($image);
}
}

/**
* Get image
*
* @return Image
*/
public function getImage()
{
return $this->image;
}
}
13 changes: 13 additions & 0 deletions Tests/Resources/app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,17 @@ public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(__DIR__.'/config/config.php');
}

/**
* Returns the kernel parameters.
*
* @return array An array of kernel parameters
*/
protected function getKernelParameters()
{
return array_merge(
parent::getKernelParameters(),
array('kernel.cmf_test_web_dir' => CMF_TEST_ROOT_DIR . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'web')
);
}
}
Binary file added Tests/Resources/app/Resources/data/cmf-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tests/Resources/app/Resources/data/testimage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions Tests/Resources/app/Resources/views/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<h3>Phpcr</h3>
<ul>
<li><a href="{{ path('phpcr_file_test') }}">File test</a></li>
<li><a href="{{ path('phpcr_image_test') }}">Image test</a></li>
</ul>
<h2>About</h2>
<p>This test application is built into the MediaBundle. You can easily run
Expand Down
2 changes: 1 addition & 1 deletion Tests/Resources/app/Resources/views/tests/file.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
{% else %}
<ul class="downloads">
{% for file in files %}
<li><a href="{{ cmf_media_download_url(file) }}" title="Download {{ file.name }}">Download {{ file.name }}</a></li>
<li><a href="{{ cmf_media_download_url(file) }}" title="Download {{ file.name }}">Download {{ file.name }} (id: {{ file.id }})</a></li>
{% endfor %}
</ul>
{% endif %}
Expand Down
Loading