Author: ornicar2
Date: 2010-02-11 18:13:13 +0100 (Thu, 11 Feb 2010)
New Revision: 27907

Added:
   
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmMedia/lib/BasedmMediaActions.class.php
   
plugins/diemPlugin/trunk/dmCorePlugin/test/project/test/functional/admin/dmGalleryTest.php
Removed:
   
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmMedia/lib/dmGalleryMediaForm.php
Modified:
   
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmMedia/actions/actions.class.php
   
plugins/diemPlugin/trunk/dmCorePlugin/lib/doctrine/extension/DmGallery/Template/DmGallery.php
   plugins/diemPlugin/trunk/dmCorePlugin/lib/form/dmFormDoctrine.php
   
plugins/diemPlugin/trunk/dmCorePlugin/lib/form/doctrine/DmMediaForRecordForm.php
   
plugins/diemPlugin/trunk/dmCorePlugin/lib/form/doctrine/PluginDmMediaForm.class.php
   
plugins/diemPlugin/trunk/dmCorePlugin/lib/model/doctrine/PluginDmMedia.class.php
   plugins/diemPlugin/trunk/dmCorePlugin/lib/os/dmOs.php
   
plugins/diemPlugin/trunk/dmCorePlugin/test/project/test/unit/dmFormEmbedMediaTest.php
Log:
[Diem]
- media library nor more replaces media files, but create a new media for each 
uploaded file
- existing medias are now intelligently suffixed
- added PluginDmMediaForm->setMimeTypeWhiteList method to ease the media form 
validation
- removed deprecated class dmGalleryMediaForm. Admin gallery now just uses 
DmMediaForm
- made admin media actions overridable, and refactored them
- added functional tests for admin galleries
- simplified handling of embedded media forms
- added unit tests for embedded media forms
- improved DmGallery Doctrine Template and added hasMedia(DmMedia) method
- removed deprecated attribute in DmMediaForRecordForm
- simplified and improved performance a bit in dmOs

Modified: 
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmMedia/actions/actions.class.php
===================================================================
--- 
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmMedia/actions/actions.class.php
    2010-02-11 17:00:10 UTC (rev 27906)
+++ 
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmMedia/actions/actions.class.php
    2010-02-11 17:13:13 UTC (rev 27907)
@@ -1,104 +1,8 @@
 <?php
 
-class dmMediaActions extends dmAdminBaseActions
+require_once(dmOs::join(sfConfig::get('dm_admin_dir').'/modules/dmMedia/lib/BasedmMediaActions.class.php'));
+
+class dmMediaActions extends BasedmMediaActions
 {
   
-  public function executeGallery(dmWebRequest $request)
-  {
-    $this->record = $this->getGalleryRecord($request);
-    
-    $this->form   = dmGalleryMediaForm::factory($this->record);
-  
-    if ($request->isMethod('post') && $this->form->bindAndValid($request))
-    {
-      if (!$media = $this->form->fileAlreadyExists())
-      {
-        $media = $this->form->save();
-      }
-      
-      $this->record->getDmGallery()->add($media);
-      
-      $this->record->save();
-      
-      $this->getUser()->logInfo('The item was updated successfully.');
-      return $this->redirectBack();
-    }
-    
-    $this->getService('bread_crumb')->setRecord($this->record);
-    
$this->context->getEventDispatcher()->connect('dm.bread_crumb.filter_links', 
array($this, 'listenToBreadCrumbFilterLinksEvent'));
-    
-    $this->galleryOptions = array(
-      'model' => get_class($this->record),
-      'pk'    => $this->record->getPrimaryKey()
-    );
-    
-    $this->medias = dmDb::query('DmMedia m, m.Folder f, 
m.'.$this->record->getGalleryRelClass().' rel')
-    ->where('rel.dm_record_id = ?', $this->record->get('id'))
-    ->orderBy('rel.position ASC')
-    ->select('m.*, f.*, rel.id as dm_gallery_rel_id')
-    ->fetchRecords();
-  }
-  
-  public function executeSortGallery(dmWebRequest $request)
-  {
-    $this
-    ->getGalleryRecord($request)
-    ->getTable()
-    ->getRelationHolder()
-    ->get('Medias')
-    ->getAssociationTable()
-    ->doSort(array_flip($request->getParameter('dm_sort')));
-    
-    return $this->renderText('ok');
-  }
-  
-  public function executeGalleryDelete(dmWebRequest $request)
-  {
-    $record = $this->getGalleryRecord($request);
-    
-    $this->forward404Unless(
-      $relObject = 
dmDb::table($record->getGalleryRelClass())->find($request->getParameter('rel_id'))
-    );
-    
-    $this->forwardSecureUnless(
-      $relObject->get('dm_record_id') === $record->get('id')
-    );
-    
-    $relObject->delete();
-    
-    $this->getUser()->logInfo($this->getI18n()->__('The item was deleted 
successfully.'));
-    
-    return $this->redirectBack();
-  }
-  
-
-  protected function getGalleryRecord(dmWebRequest $request)
-  {
-    $this->forward404Unless(
-      $record = 
dmDb::table($request->getParameter('model'))->find($request->getParameter('pk')),
-      'Record not found'
-    );
-
-    $this->forward404Unless(
-      $module = $record->getDmModule(),
-      'Module not found'
-    );
-
-    $this->forwardSecureUnless($this->getUser()->canAccessToModule($module));
-    
-    if (!$record->getTable()->hasTemplate('DmGallery'))
-    {
-      throw new dmException($record.' should act as DmGallery');
-    }
-    
-    return $record;
-  }
-  
-  public function listenToBreadCrumbFilterLinksEvent(sfEvent $event, array 
$links)
-  {
-    $links[] = $this->getHelper()->tag('h1', $this->getI18n()->__('Gallery'));
-    
-    return $links;
-  }
-  
 }
\ No newline at end of file

Added: 
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmMedia/lib/BasedmMediaActions.class.php
===================================================================
--- 
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmMedia/lib/BasedmMediaActions.class.php
                             (rev 0)
+++ 
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmMedia/lib/BasedmMediaActions.class.php
     2010-02-11 17:13:13 UTC (rev 27907)
@@ -0,0 +1,101 @@
+<?php
+
+class BasedmMediaActions extends dmAdminBaseActions
+{
+  
+  public function executeGallery(dmWebRequest $request)
+  {
+    $this->record = $this->getGalleryRecord($request);
+
+    $this->form = new DmMediaForm();
+    $this->form->setDefault('dm_media_folder_id', 
$this->record->getDmMediaFolder()->get('id'));
+    $this->form->setMimeTypeWhiteList('web_images');
+
+    if ($request->isMethod('post') && $this->form->bindAndValid($request))
+    {
+      $media = $this->form->save();
+
+      $this->record->addMedia($media);
+      
+      $this->getUser()->logInfo('The item was updated successfully.');
+      return $this->redirectBack();
+    }
+    
+    $this->getService('bread_crumb')->setRecord($this->record);
+    
$this->context->getEventDispatcher()->connect('dm.bread_crumb.filter_links', 
array($this, 'listenToBreadCrumbFilterLinksEvent'));
+    
+    $this->galleryOptions = array(
+      'model' => get_class($this->record),
+      'pk'    => $this->record->getPrimaryKey()
+    );
+    
+    $this->medias = dmDb::query('DmMedia m, m.Folder f, 
m.'.$this->record->getGalleryRelClass().' rel')
+    ->where('rel.dm_record_id = ?', $this->record->get('id'))
+    ->orderBy('rel.position ASC')
+    ->select('m.*, f.*, rel.id as dm_gallery_rel_id')
+    ->fetchRecords();
+  }
+  
+  public function executeSortGallery(dmWebRequest $request)
+  {
+    $this
+    ->getGalleryRecord($request)
+    ->getTable()
+    ->getRelationHolder()
+    ->get('Medias')
+    ->getAssociationTable()
+    ->doSort(array_flip($request->getParameter('dm_sort')));
+    
+    return $this->renderText('ok');
+  }
+  
+  public function executeGalleryDelete(dmWebRequest $request)
+  {
+    $record = $this->getGalleryRecord($request);
+    
+    $this->forward404Unless(
+      $relObject = 
dmDb::table($record->getGalleryRelClass())->find($request->getParameter('rel_id'))
+    );
+    
+    $this->forwardSecureUnless(
+      $relObject->get('dm_record_id') === $record->get('id')
+    );
+    
+    $relObject->delete();
+    
+    $this->getUser()->logInfo($this->getI18n()->__('The item was deleted 
successfully.'));
+    
+    return $this->redirectBack();
+  }
+  
+
+  protected function getGalleryRecord(dmWebRequest $request)
+  {
+    $this->forward404Unless(
+      $record = 
dmDb::table($request->getParameter('model'))->find($request->getParameter('pk')),
+      'Record not found'
+    );
+
+    $this->forward404Unless(
+      $module = $record->getDmModule(),
+      'Module not found'
+    );
+
+    $this->forwardSecureUnless($this->getUser()->canAccessToModule($module));
+    
+    if (!$record->getTable()->hasTemplate('DmGallery'))
+    {
+      throw new dmException($record.' should act as DmGallery');
+    }
+    
+    return $record;
+  }
+  
+  public function listenToBreadCrumbFilterLinksEvent(sfEvent $event, array 
$links)
+  {
+    $links[] = $this->getHelper()->tag('h1', $this->getI18n()->__('Gallery'));
+    
+    return $links;
+  }
+  
+}
\ No newline at end of file

Deleted: 
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmMedia/lib/dmGalleryMediaForm.php
===================================================================
--- 
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmMedia/lib/dmGalleryMediaForm.php
   2010-02-11 17:00:10 UTC (rev 27906)
+++ 
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmMedia/lib/dmGalleryMediaForm.php
   2010-02-11 17:13:13 UTC (rev 27907)
@@ -1,53 +0,0 @@
-<?php
-
-class dmGalleryMediaForm extends DmMediaForm
-{
-  protected
-  $record,
-  $fileAlreadyExists;
-
-  public static function factory(myDoctrineRecord $record)
-  {
-    $form = new self();
-    
-    $form->setDefault('dm_media_folder_id', 
$record->getDmMediaFolder()->get('id'));
-    
-    $form->setRecord($record);
-    
-    return $form;
-  }
-
-  public function resetFormFields()
-  {
-    parent::resetFormFields();
-    $this->fileAlreadyExists = null;
-  }
-  
-  public function checkExistingNameInParent($validator, $values)
-  {
-    if (!empty($values['file']))
-    {
-      $values['dm_media_folder_id'] = 
$this->record->getDmMediaFolder()->get('id');
-    }
-    
-    return parent::checkExistingNameInParent($validator, $values);
-  }
-  
-  protected function setRecord(dmDoctrineRecord $record)
-  {
-    $this->record = $record;
-  }
-
-  protected function throwFileAlreadyExists($validator, $folder, $filename)
-  {
-    $this->fileAlreadyExists = dmDb::query('DmMedia m')
-    ->where('m.dm_media_folder_id = ? AND m.file = ?', 
array($folder->get('id'), $filename))
-    ->fetchRecord();
-  }
-
-  public function fileAlreadyExists()
-  {
-    return $this->fileAlreadyExists;
-  }
-
-}
\ No newline at end of file

Modified: 
plugins/diemPlugin/trunk/dmCorePlugin/lib/doctrine/extension/DmGallery/Template/DmGallery.php
===================================================================
--- 
plugins/diemPlugin/trunk/dmCorePlugin/lib/doctrine/extension/DmGallery/Template/DmGallery.php
       2010-02-11 17:00:10 UTC (rev 27906)
+++ 
plugins/diemPlugin/trunk/dmCorePlugin/lib/doctrine/extension/DmGallery/Template/DmGallery.php
       2010-02-11 17:13:13 UTC (rev 27907)
@@ -34,11 +34,20 @@
       'refClass' => $this->_plugin->getTable()->getOption('name')
     )), Doctrine_Relation::MANY);
   }
-  
+
   public function hasMedias()
   {
     return $this->getNbMedias() > 0;
   }
+
+  public function hasMedia(DmMedia $media)
+  {
+    return dmDb::table($this->getGalleryRelClass())
+    ->createQuery('r')
+    ->where('r.dm_record_id = ?', $this->getInvoker()->get('id'))
+    ->andWhere('r.dm_media_id = ?', $media->get('id'))
+    ->exists();
+  }
   
   public function getNbMedias()
   {
@@ -47,26 +56,43 @@
   
   public function addMedia(DmMedia $media)
   {
-    $this->getDmGallery()->add($media);
+    if(!$this->hasMedia($media))
+    {
+      $rel = dmDb::table($this->getGalleryRelClass())->create(array(
+        'dm_media_id' => $media->get('id'),
+        'dm_record_id' => $this->getInvoker()->get('id')
+      ));
+
+      $rel->save();
+    }
     
-    return $this->_invoker;
+    return $this->getInvoker();
   }
   
   public function addMedias($medias)
   {
-    $currentMedias = $this->getDmGallery();
-    
     foreach($medias as $media)
     {
-      $currentMedias->add($media);
+      $this->addMedia($media);
     }
-    
-    return $this->_invoker;
+
+    return $this->getInvoker();
   }
 
-  public function getDmGallery()
+  public function removeMedias()
   {
-    if (!$medias = $this->_invoker->reference($this->_options['mediaAlias']))
+    dmDb::table($this->getGalleryRelClass())
+    ->createQuery()
+    ->delete()
+    ->where('dm_record_id = ?', $this->_invoker->get('id'))
+    ->execute();
+
+    return $this->getInvoker();
+  }
+
+  public function getDmGallery($reload = false)
+  {
+    if ($reload || (!$medias = 
$this->_invoker->reference($this->_options['mediaAlias'])))
     {
       $medias = dmDb::query('DmMedia m, m.Folder f, 
m.'.$this->getGalleryRelClass().' rel')
       ->where('rel.dm_record_id = ?', $this->_invoker->get('id'))

Modified: plugins/diemPlugin/trunk/dmCorePlugin/lib/form/dmFormDoctrine.php
===================================================================
--- plugins/diemPlugin/trunk/dmCorePlugin/lib/form/dmFormDoctrine.php   
2010-02-11 17:00:10 UTC (rev 27906)
+++ plugins/diemPlugin/trunk/dmCorePlugin/lib/form/dmFormDoctrine.php   
2010-02-11 17:13:13 UTC (rev 27907)
@@ -70,38 +70,18 @@
       return $values;
     }
 
-    // uploading a file
-    if($values[$formName]['file'])
+    /*
+     * We have a new file for an existing media.
+     * Let's create a new media
+     */
+    if($values[$formName]['file'] && $values[$formName]['id'])
     {
-      $values[$formName]['dm_media_folder_id'] = 
$this->object->getTable()->getDmMediaFolder()->get('id');
-      
-      $existingMedia = dmDb::query('DmMedia m')
-      ->where('m.dm_media_folder_id = ?', 
$values[$formName]['dm_media_folder_id'])
-      ->andWhere('m.file = ?', $values[$formName]['file']->getOriginalName())
-      ->fetchRecord();
-      /*
-       * We have a media with same folder / filename
-       * let's reuse the media, and replace the file
-       */
-      if ($existingMedia)
-      {
-        $values[$formName]['id'] = $existingMedia->get('id');
+      $values[$formName]['id'] = null;
         
-        $this->embeddedForms[$formName]->setObject($existingMedia);
-      }
-      /*
-       * We have a new file for an existing media.
-       * Let's create a new media
-       */
-      elseif($values[$formName]['id'])
-      {
-        $values[$formName]['id'] = null;
-        
-        $media = new DmMedia;
-        $media->Folder = $this->object->getDmMediaFolder();
-  
-        $this->embeddedForms[$formName]->setObject($media);
-      }
+      $media = new DmMedia;
+      $media->Folder = $this->object->getDmMediaFolder();
+
+      $this->embeddedForms[$formName]->setObject($media);
     }
     
     return $values;

Modified: 
plugins/diemPlugin/trunk/dmCorePlugin/lib/form/doctrine/DmMediaForRecordForm.php
===================================================================
--- 
plugins/diemPlugin/trunk/dmCorePlugin/lib/form/doctrine/DmMediaForRecordForm.php
    2010-02-11 17:00:10 UTC (rev 27906)
+++ 
plugins/diemPlugin/trunk/dmCorePlugin/lib/form/doctrine/DmMediaForRecordForm.php
    2010-02-11 17:13:13 UTC (rev 27907)
@@ -3,8 +3,7 @@
 class DmMediaForRecordForm extends DmMediaForm
 {
   protected
-  $record,
-  $fileAlreadyExists;
+  $record;
 
   public static function factory(myDoctrineRecord $record, $local, $alias, 
$required)
   {
@@ -27,12 +26,6 @@
     $form->setRecord($record);
     return $form;
   }
-
-  public function resetFormFields()
-  {
-    parent::resetFormFields();
-    $this->fileAlreadyExists = null;
-  }
   
   public function checkFolder($validator, $values)
   {

Modified: 
plugins/diemPlugin/trunk/dmCorePlugin/lib/form/doctrine/PluginDmMediaForm.class.php
===================================================================
--- 
plugins/diemPlugin/trunk/dmCorePlugin/lib/form/doctrine/PluginDmMediaForm.class.php
 2010-02-11 17:00:10 UTC (rev 27906)
+++ 
plugins/diemPlugin/trunk/dmCorePlugin/lib/form/doctrine/PluginDmMediaForm.class.php
 2010-02-11 17:13:13 UTC (rev 27907)
@@ -26,6 +26,11 @@
     $this->mergePostValidator(new sfValidatorCallback(array('callback' => 
array($this, 'clearName'))));
     $this->mergePostValidator(new sfValidatorCallback(array('callback' => 
array($this, 'checkFolder'))));
   }
+  
+  public function setMimeTypeWhiteList($mimeTypes)
+  {
+    $this->validatorSchema['file']->setOption('mime_types', $mimeTypes);
+  }
 
   protected function doUpdateObject($values)
   {
@@ -50,20 +55,7 @@
 
     if ($validatedFile)
     {
-      if ($this->object->isNew())
-      {
-        if (!$this->object->create($validatedFile))
-        {
-          throw new dmException(sprintf('Can not create file for media %s', 
$this->object));
-        }
-      }
-      else
-      {
-        if (!$this->object->replaceFile($validatedFile))
-        {
-          throw new dmException(sprintf('Can not replace file for media %s', 
$object));
-        }
-      }
+      $values = $this->handleValidatedFile($validatedFile, $values);
     }
 
     if(isset($moveToFolderId))
@@ -72,6 +64,31 @@
     }
   }
 
+  /*
+   * By default, when a file is uploaded
+   * 1. If media is new, create the file
+   * 2. If media already exists with another file, keep the media and replace 
the file
+   */
+  protected function handleValidatedFile(sfValidatedFile $file, array $values)
+  {
+    if ($this->object->isNew())
+    {
+      if (!$this->object->create($file))
+      {
+        throw new dmException(sprintf('Can not create file for media %s', 
$this->object));
+      }
+    }
+    else
+    {
+      if (!$this->object->replaceFile($file))
+      {
+        throw new dmException(sprintf('Can not replace file for media %s', 
$object));
+      }
+    }
+
+    return $values;
+  }
+
   public function clearName($validator, $values)
   {
     if (!empty($values['file']))
@@ -100,7 +117,7 @@
 
       if(!is_writable($folder->fullPath))
       {
-        $error = new sfValidatorError($validator, 
dmProject::unRootify($folder->fullPath)." is not writable");
+        $error = new sfValidatorError($validator, 
dmProject::unRootify($folder->fullPath).' is not writable');
 
         // throw an error bound to the file field
         throw new sfValidatorErrorSchema($validator, array('file' => $error));

Modified: 
plugins/diemPlugin/trunk/dmCorePlugin/lib/model/doctrine/PluginDmMedia.class.php
===================================================================
--- 
plugins/diemPlugin/trunk/dmCorePlugin/lib/model/doctrine/PluginDmMedia.class.php
    2010-02-11 17:00:10 UTC (rev 27906)
+++ 
plugins/diemPlugin/trunk/dmCorePlugin/lib/model/doctrine/PluginDmMedia.class.php
    2010-02-11 17:13:13 UTC (rev 27907)
@@ -169,7 +169,7 @@
    */
   public function create(sfValidatedFile $file)
   {
-    $this->file = dmOs::sanitizeFileName($file->getOriginalName());
+    $this->file = 
$this->getAvailableFileName(dmOs::sanitizeFileName($file->getOriginalName()));
 
     $this->clearCache();
 
@@ -190,6 +190,35 @@
     return $this->create($file);
   }
 
+  /*
+   * if this file already exists in the folder,
+   * add a numeric suffix not to override the first one
+   */
+  protected function getAvailableFileName($fileName)
+  {
+    if(!$this->get('Folder')->hasFile($fileName))
+    {
+      return $fileName;
+    }
+
+    $name = pathinfo($fileName, PATHINFO_FILENAME);
+    $extension = pathinfo($fileName, PATHINFO_EXTENSION);
+
+    if(!preg_match('/_\d+$/', $name))
+    {
+      $name .= '_1';
+    }
+    $number = (int) preg_replace('/.+_(\d+)$/', '$1', $name);
+
+    while($this->get('Folder')->hasFile($name.'.'.$extension))
+    {
+      ++$number;
+      $name = preg_replace('/(.+)_\d+$/', '$1_'.$number, $name);
+    }
+
+    return $name.'.'.$extension;
+  }
+
   /**
    * @return DmMedia the new media with $toMedia values
    */
@@ -317,27 +346,9 @@
       throw new dmException(sprintf('Trying to save DmMedia with no existing 
file : %s', $this->file));
     }
 
-    /*
-     * If this media is new, and shares its name with another media in the 
same folder,
-     * this media is not saved and the other one is updated with this media's 
value
-     */
     if($this->isNew())
     {
-      if($sameMedia = 
$this->getTable()->findOneByFileAndDmMediaFolderId($this->file, 
$this->dm_media_folder_id))
-      {
-        return $sameMedia
-        ->setLegend($this->getLegend())
-        ->setAuthor($this->getAuthor())
-        ->setLicense($this->getLicense())
-        ->setMime($this->getMime())
-        ->setSize($this->getSize())
-        ->set('dimensions', $this->getDimensions(), false)
-        ->save($conn);
-      }
-      else
-      {
-        $this->refreshFromFile();
-      }
+      $this->refreshFromFile();
     }
 
     return parent::save($conn);

Modified: plugins/diemPlugin/trunk/dmCorePlugin/lib/os/dmOs.php
===================================================================
--- plugins/diemPlugin/trunk/dmCorePlugin/lib/os/dmOs.php       2010-02-11 
17:00:10 UTC (rev 27906)
+++ plugins/diemPlugin/trunk/dmCorePlugin/lib/os/dmOs.php       2010-02-11 
17:13:13 UTC (rev 27907)
@@ -79,13 +79,13 @@
 
   public static function getFileExtension($file, $widthDot = true)
   {
-    $extension = dmArray::get(pathinfo($file), 'extension');
+    $extension = pathinfo($file, PATHINFO_EXTENSION);
     return $widthDot ? '.'.$extension : $extension;
   }
 
   public static function getFileWithoutExtension($file)
   {
-    return dmArray::get(pathinfo($file), 'filename');
+    return pathinfo($file, PATHINFO_FILENAME);
   }
 
   public static function sanitizeDirName($dirName)

Added: 
plugins/diemPlugin/trunk/dmCorePlugin/test/project/test/functional/admin/dmGalleryTest.php
===================================================================
--- 
plugins/diemPlugin/trunk/dmCorePlugin/test/project/test/functional/admin/dmGalleryTest.php
                          (rev 0)
+++ 
plugins/diemPlugin/trunk/dmCorePlugin/test/project/test/functional/admin/dmGalleryTest.php
  2010-02-11 17:13:13 UTC (rev 27907)
@@ -0,0 +1,70 @@
+<?php
+
+require_once(realpath(dirname(__FILE__).'/../../../..').'/functional/helper/dmFunctionalTestHelper.php');
+$helper = new dmFunctionalTestHelper();
+$helper->boot('admin');
+
+$browser = $helper->getBrowser();
+
+$helper->login();
+
+$browser->info('Posts list')
+->get('/content/blog/dm-test-posts/index')
+->checks(array(
+  'moduleAction' => 'dmTestPost/index'
+))
+->click('tbody td.sf_admin_text:first a')
+->checks(array(
+  'moduleAction' => 'dmTestPost/edit'
+));
+
+$browser->info('Remove all post medias');
+$post = 
dmDb::table('DmTestPost')->find($browser->getRequest()->getParameter('pk'));
+$post->removeMedias();
+
+$browser->click('Edit medias')
+->checks(array(
+  'moduleAction' => 'dmMedia/gallery'
+))
+->has('.dm_gallery_big ul.list li', false)
+->info('Submit invalid form')
+->click('form.dm_add_media input.submit', array('dm_gallery_media_form' => 
array(
+
+)))
+->checks(array(
+  'moduleAction' => 'dmMedia/gallery',
+  'code' => 200,
+  'method' => 'post'
+))
+->has('form.dm_add_media ul.error_list li', 'Required.')
+->info('Submit valid form')
+->click('form.dm_add_media input.submit', array('dm_media_form' => array(
+  'dm_media_folder_id' => $post->getDmMediaFolder()->id,
+  'file' => dmOs::join(sfConfig::get('dm_core_dir'), 
'test/fixtures/uploads/images/deeper/firefox.jpg')
+)))
+->checks(array(
+  'moduleAction' => 'dmMedia/gallery',
+  'code' => 302,
+  'method' => 'post'
+))
+->redirect()
+->checks(array(
+  'moduleAction' => 'dmMedia/gallery',
+  'code' => 200,
+  'method' => 'get'
+))
+->has('.dm_gallery_big ul.list li img')
+->info('Remove file')
+->click('.dm_gallery_big ul.list li a.delete')
+->checks(array(
+  'moduleAction' => 'dmMedia/galleryDelete',
+  'code' => 302,
+  'method' => 'get'
+))
+->redirect()
+->checks(array(
+  'moduleAction' => 'dmMedia/gallery',
+  'code' => 200,
+  'method' => 'get'
+))
+->has('.dm_gallery_big ul.list li img', false);
\ No newline at end of file

Modified: 
plugins/diemPlugin/trunk/dmCorePlugin/test/project/test/unit/dmFormEmbedMediaTest.php
===================================================================
--- 
plugins/diemPlugin/trunk/dmCorePlugin/test/project/test/unit/dmFormEmbedMediaTest.php
       2010-02-11 17:00:10 UTC (rev 27906)
+++ 
plugins/diemPlugin/trunk/dmCorePlugin/test/project/test/unit/dmFormEmbedMediaTest.php
       2010-02-11 17:13:13 UTC (rev 27907)
@@ -7,7 +7,7 @@
 $categ = dmDb::create('DmTestCateg', array('name' => 
dmString::random()))->saveGet();
 $user = dmDb::table('DmUser')->findOne();
 
-$t = new lime_test(40);
+$t = new lime_test(44);
 
 $t->comment('Create a post without media');
 
@@ -160,11 +160,17 @@
 
 $t->is($post->Image->exists(), true, 'The post has an existing media');
 
-$t->is($post->Image->id, $media1->id, 'The same old media were reused');
+$t->isnt($post->Image->id, $media1->id, 'The same old media were NOT reused');
+$media1Bis = $post->Image;
 
-$t->is($post->Image->file, $media1FileName, 'Post media filename is 
'.$media1FileName);
+$t->is($post->Image->file, $expected = str_replace('.jpg', '_1.jpg', 
$media1FileName), 'Post media filename is '.$expected);
 $t->is($post->Image->size, filesize($media1FullPath), 'Post media size is 
'.filesize($media1FullPath));
 
+$t->ok($media1->exists(), 'media1 still exists');
+$t->is($media1->file, $media1FileName, 'media1 file name is '.$media1FileName);
+$t->ok($media1Bis->exists(), 'media1Bis exists');
+$t->is($media1Bis->file, $expected = str_replace('.jpg', '_1.jpg', 
$media1FileName), 'media1Bis file name is '.$expected);
+
 $t->comment('Upload a new media to the post');
 
 $form = new DmTestPostForm($post);
@@ -250,12 +256,12 @@
 $t->is($post->Image->exists(), true, 'The post has an existing media');
 
 $media3 = $post->Image;
-$t->is($media3->file, $media3FileName, 'Post media filename is 
'.$media3FileName);
+$t->is($media3->file, $expected = str_replace('.jpg', '_1.jpg', 
$media3FileName), 'Post media filename is '.$expected);
 $t->is($media3->size, filesize($media3FullPath), 'Post media size is 
'.filesize($media3FullPath));
 
-$t->is($media2->id, $media3->id, 'The new media is the same');
+$t->isnt($media2->id, $media3->id, 'The new media is NOT the same');
 
-$t->is($media2->file, $media3->file, 'The media filename is the same');
+$t->isnt($media2->file, $media3->file, 'The media filename is NOT the same');
 
 $t->isnt($media2Size, $media3->size, 'The media file has changed');
 

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/symfony-svn?hl=en.

Reply via email to