/** * Move into another folder * * @param DmMediaFolder $folder */ public function move(DmMediaFolder $folder) { if ($folder->id == $this->nodeParentId) { return $this; } if ($folder->getNode()->isDescendantOfOrEqualTo($this)) { throw new dmException('Can not move to a descendant'); } if ($this->getNode()->isRoot()) { throw new dmException('The root folder cannot be moved'); } if (!$this->isWritable()) { throw new dmException(sprintf('The folder %s is not writable.', dmProject::unRootify($this->fullPath))); } if (!$folder->isWritable()) { throw new dmException(sprintf('The folder %s is not writable.', dmProject::unRootify($folder->fullPath))); } if ($folder->hasSubFolder($this->name)) { throw new dmException(sprintf('The selected folder already contains a folder named "%s".', $this->name)); } $oldRelPath = $this->get('rel_path'); $newRelPath = $folder->get('rel_path') . '/' . $this->name; $fs = $this->getService('filesystem'); $oldFullPath = $this->getFullPath(); $newFullPath = dmOs::join($folder->getFullPath(), $this->name); if (!rename($oldFullPath, $newFullPath)) { throw new dmException('Can not move %s to %s', dmProject::unRootify($oldFullPath), dmProject::unRootify($newFullPath)); } $this->set('rel_path', $newRelPath); $this->clearCache(); $this->getNode()->moveAsFirstChildOf($folder); //update descendants if ($descendants = $this->getNode()->getDescendants()) { foreach ($descendants as $folder) { $folder->set('rel_path', dmString::str_replace_once($oldRelPath, $newRelPath, $folder->get('rel_path'))); $folder->save(); } } return $this; }
$t = new lime_test(58); $mediaTable = dmDb::table('DmMedia'); $folderTable = dmDb::table('DmMediaFolder'); $t->diag('Media tests'); $folderTable->checkRoot(); $root = $folderTable->getTree()->fetchRoot(); $t->diag('syncing root'); $root->sync(); $helper->checkTreeIntegrity($t); $helper->testFolderCorrelations($t); $t->isa_ok($root, 'DmMediaFolder', 'root is a media folder'); $t->is($root->fullPath, dmOs::normalize(sfConfig::get('sf_upload_dir')), 'root full path is ' . $root->fullPath); $t->diag('add a folder in root'); $folder = new DmMediaFolder(); $folder->relPath = 'test_' . dmString::random(8); $folder->getNode()->insertAsLastChildOf($root); $helper->checkTreeIntegrity($t); $t->ok($folder->exists(), 'folder ' . $folder->name . ' has been created'); $t->is($folder->getNode()->getParent(), $root, 'folder\'s parent is root'); $t->is($folder->fullPath, dmOs::join(sfConfig::get('sf_upload_dir'), $folder->name), 'folder\'s full path is ' . $folder->fullPath); $t->ok(is_dir($folder->fullPath), 'folder exists in filesystem'); $t->diag('add a file in folder'); $fileName = dmString::random(8) . '_' . basename(__FILE__); $filePath = dmOs::join($folder->fullPath, $fileName); copy(__FILE__, $filePath); $media = $mediaTable->create(array('file' => basename($filePath), 'author' => 'Thibault D.', 'legend' => 'dmMedia test cases', 'dm_media_folder_id' => $folder->id))->saveGet(); $t->ok($media->exists(), 'media has been saved'); $t->is($media->fullPath, $folder->fullPath . '/' . $media->file, 'Media full path is ' . $media->fullPath); $t->is($media->relPath, $folder->relPath . '/' . $media->file, 'Media rel path is ' . $media->relPath); $t->is($media->webPath, 'uploads/' . $media->relPath, 'Media web path is ' . $media->webPath); $t->is($media->Folder, $folder, 'media folder is folder');