* @package     lyMediaManagerPlugin
 * @subpackage  functional tests
 * @copyright   Copyright (C) 2010 Massimo Giagnoni.
 * @license     http://www.symfony-project.org/license MIT
 * @version     SVN: $Id$
 */
include dirname(__FILE__) . '/../bootstrap/functional.php';
Doctrine_Core::loadData(dirname(__FILE__) . '/../data/fixtures/fixtures_fs.yml');
$root = Doctrine::getTable('lyMediaFolder')->findOneByName('media');
$subf1 = lyMediaFolderTable::getInstance()->findOneByName('testsub1');
$subf2 = lyMediaFolderTable::getInstance()->findOneByName('testsub2');
//Creates some test assets. Cannot be done in fixtures
$file = dirname(__FILE__) . '/../data/assets/asset1.png';
$asset = new lyMediaAsset();
$asset->setFolder($subf1);
$asset->setFilename($file);
$asset->save();
$asset->refresh();
$file = dirname(__FILE__) . '/../data/assets/asseta.png';
$asset2 = new lyMediaAsset();
$asset2->setFolder($subf2);
$asset2->setFilename($file);
$asset2->save();
$asset2->refresh();
$base = sfConfig::get('sf_web_dir') . DIRECTORY_SEPARATOR . 'media' . DIRECTORY_SEPARATOR;
chmod($base . 'testsub1', 0555);
$browser = new lyMediaTestFunctional(new sfBrowser());
$browser->setTester('doctrine', 'sfTesterDoctrine');
$browser->info('1 - Unwritable destination folder')->info('  1.1 - Create folder')->get('/ly_media_asset')->click('li.sf_admin_action_new_folder a')->click('li.sf_admin_action_save input', array('ly_media_folder' => array('parent_id' => $subf1->getId(), 'name' => 'test')))->with('form')->begin()->hasErrors(1)->hasGlobalError('parent_unwritable')->end()->info('  1.2 - Move folder')->get('/ly_media_folder/' . $subf2->getId() . '/edit')->click('li.sf_admin_action_save input', array('ly_media_folder' => array('parent_id' => $subf1->getId())))->with('form')->begin()->hasErrors(1)->hasGlobalError('parent_unwritable')->end()->info('  1.3 - Upload asset')->get('/ly_media_asset')->click('li.sf_admin_action_new a')->click('li.sf_admin_action_save input', array('ly_media_asset' => array('folder_id' => $subf1->getId(), 'title' => 'test', 'filename' => dirname(__FILE__) . '/../data/assets/asset1.png')))->with('form')->begin()->hasErrors(1)->hasGlobalError('folder_unwritable')->end()->info('  1.4 - Rename asset')->get('/ly_media_asset/' . $asset->getId() . '/edit')->click('li.sf_admin_action_save input', array('filename' => 'renamed'))->with('form')->begin()->hasErrors(1)->hasGlobalError('folder_unwritable')->end()->info('  1.5 - Delete asset')->get('/ly_media_asset')->click('li.sf_admin_action_delete a[href$=ly_media_asset/' . $asset->getId() . ']', array(), array('method' => 'delete', '_with_csrf' => true))->with('response')->isRedirected()->followRedirect()->with('response')->checkElement('div.error:contains("asset1.png")')->info('  1.6 - Move asset')->get('/ly_media_asset/' . $asset2->getId() . '/edit')->click('li.sf_admin_action_save input', array('ly_media_asset' => array('folder_id' => $subf1->getId())))->with('form')->begin()->hasErrors(1)->hasGlobalError('folder_unwritable')->end();
//Make writable to allow removal
chmod($base . 'testsub1', 0755);
$t->info('Create asset');
$file = dirname(__FILE__) . '/../data/assets/asset1.png';
$a = new lyMediaAsset();
$a->setFolder($root);
$a->setFilename($file);
$a->save();
$a->refresh();
$t->is($a->getFilename(), 'asset1.png', '->getFilename()');
$t->is($a->getType(), 'image/png', '->getType()');
$t->is($a->getPath(), 'test_root/asset1.png', '->getPath()');
$t->ok(file_exists($base . 'asset1.png'), 'File exists');
$t->ok(file_exists($base . 'thumbs/small_asset1.png'), 'Small thumbnail exists');
$t->ok(file_exists($base . 'thumbs/medium_asset1.png'), 'Medium thumbnail exists');
$t->info('Create another asset with the same file');
$a2 = new lyMediaAsset();
$a2->setFolder($root);
$a2->setFilename($file);
$a2->save();
$a2->refresh();
$t->is($a2->getFilename(), 'asset1(1).png', 'Filename is unique');
$t->info('Rename asset: asset1.png > renamed.png');
$a->setFilename('renamed.png');
$a->save();
$a->refresh();
$t->is($a->getPath(), 'test_root/renamed.png', '->getPath()');
$t->ok(file_exists($base . 'renamed.png'), 'Renamed file exists');
$t->ok(file_exists($base . 'thumbs/small_renamed.png'), 'Small thumbnail is renamed');
$t->ok(file_exists($base . 'thumbs/medium_renamed.png'), 'Medium thumbnail is renamed');
$t->ok(!file_exists($base . 'asset1.png'), 'Old file name does not exist');
$t->ok(!file_exists($base . 'thumbs/small_asset1.png'), 'Old small thumbnail does not exist');
$t->ok(!file_exists($base . 'thumbs/medium_asset1.png'), 'Old medium thumbnail does not exist');
$t->info('Move folder deeper');
$folder2->move($sub);
$folder2->refresh();
/*
 * test_root
 *  -- test
 *  -- -- test-sub
 *  -- -- -- test 2
 */
$t->is($folder2->getRelativePath(), 'test_root/test/test-sub/test2/', '->getRelativePath()');
$t->is($folder2->getNode()->getPath('/', true), 'test_root/test/test-sub/test2', 'Folder has right path');
$t->ok($fs->is_dir($folder2->getRelativePath()), 'Folder exists in filesystem');
$t->info('Synchronize folder');
//Create orphaned asset
$asset = new lyMediaAsset();
$asset->setFolder($folder);
$asset->setFilename(dirname(__FILE__) . '/../data/assets/asseta.png');
$asset->save();
$root->synchronizeWith(dirname(__FILE__) . '/../data/assets/', false);
$asset = lyMediaAssetTable::getInstance()->findOneByFilename('asset1.png');
$t->ok(is_object($asset), 'Asset created in root');
$t->is($asset->getPath(), 'test_root/asset1.png', 'Asset has correct path');
$t->ok($fs->is_file('test_root/asset1.png'), 'Asset exists in filesystem');
$t->ok($fs->is_file('test_root/thumbs/small_asset1.png'), 'Small thumbnail exists in filesystem');
$t->ok($fs->is_file('test_root/thumbs/medium_asset1.png'), 'Medium thumbnail exists in filesystem');
$asset = lyMediaAssetTable::getInstance()->findOneByFilename('assetc.png');
$t->ok(is_object($asset), 'Asset created in subfolder');
$t->is($asset->getPath(), 'test_root/test/test-sub/assetc.png', 'Asset has correct path');
$t->ok($fs->is_file('test_root/test/test-sub/assetc.png'), 'Asset exists in filesystem');
$t->ok($fs->is_file('test_root/test/test-sub/thumbs/small_assetc.png'), 'Small thumbnail exists in filesystem');
$t->ok($fs->is_file('test_root/test/test-sub/thumbs/medium_assetc.png'), 'Medium thumbnail exists in filesystem');
$t->info('Move *and* rename asset');
$a->setFilename('asset.png');
$a->setFolder($root);
$a->save();
$a->refresh();
$t->is($a->getPath(), 'test_root/asset.png', '->getPath()');
$t->ok(file_exists($base . 'asset.png'), 'Moved/renamed file exists');
$t->ok(file_exists($base . 'thumbs/small_asset.png'), 'Small thumbnail is moved/renamed');
$t->ok(file_exists($base . 'thumbs/medium_asset.png'), 'Medium thumbnail is moved/renamed');
$t->ok(!file_exists($base . 'test/renamed.png'), 'File does not exist in old path');
$t->ok(!file_exists($base . 'test/thumbs/small_renamed.png'), 'Small thumbnail does not exist in old path');
$t->ok(!file_exists($base . 'test/thumbs/medium_renamed.png'), 'Medium thumbnail does not exist in old path');
$t->info('Delete asset');
$a->delete();
$t->ok(!file_exists($base . 'asset.png'), 'deleted file does not exist');
$t->ok(!file_exists($base . 'thumbs/small_asset.png'), 'Deleted small thumbnail does not exist');
$t->ok(!file_exists($base . 'thumbs/medium_asset.png'), 'Deleted medium thumbnail does not exist');
$t->info('Create thumbnails');
//Turn off automatic thumbnail generation
sfConfig::set('app_lyMediaManager_create_thumbnails_for', array());
$a = new lyMediaAsset();
$a->setFolder($root);
$a->setFilename(dirname(__FILE__) . '/../data/assets/asseta.png');
$a->save();
$a->refresh();
$t->ok(!file_exists($base . 'thumbs/small_asseta.png'), 'small thumbnail does not exist');
$t->ok(!file_exists($base . 'thumbs/medium_asseta.png'), 'medium thumbnail does not exist');
sfConfig::set('app_lyMediaManager_create_thumbnails_for', array('image/png'));
$a->generateThumbnails();
$t->ok(file_exists($base . 'thumbs/small_asseta.png'), 'small thumbnail exists');
$t->ok(file_exists($base . 'thumbs/medium_asseta.png'), 'medium thumbnail exists');