/** * @role update */ public function create() { $product = Product::getInstanceByID((int) $this->request->get('productID')); if ($uploadFile = $this->request->get('uploadFile')) { $tmpPath = $uploadFile['tmp_name']; $name = $uploadFile['name']; } else { $tmpPath = null; $name = basename($this->request->get('filePath')); } $productFile = ProductFile::getNewInstance($product, $tmpPath, $name, $this->request->get('filePath')); return $this->save($productFile); }
public function testDeleteFileGroupWithFiles() { $group = ProductFileGroup::getNewInstance($this->product); $group->setNextPosition(); $group->setValueByLang('name', 'en', 'TEST_GROUP'); $group->save(); $file = ClassLoader::getRealPath('cache.') . 'blabla'; file_put_contents($file, 'asdsad'); $productFile = ProductFile::getNewInstance($this->product, $file, 'movedFile.txt'); $productFile->productFileGroup->set($group); $productFile->save(); $productFilePath = $productFile->getPath(); $group->delete(); try { $productFile->reload(); $this->fail(); } catch (Exception $e) { $this->pass(); } $this->assertFalse(is_file($productFilePath)); unlink($file); }
public function testMergeGroupsWithFIlters() { // create groups $productGroup = array(); foreach (range(1, 3) as $i) { $productGroup[$i] = ProductFileGroup::getNewInstance($this->product); $productGroup[$i]->save(); } // create files $productFile = array(); $productFiles = array(); $dir = ClassLoader::getRealPath('cache') . '/'; foreach (range(1, 2) as $i) { file_put_contents($productFiles[$i] = $dir . md5($i), "file {$i}"); $productFile[$i] = ProductFile::getNewInstance($this->product, $productFiles[$i], "test_file_{$i}.txt"); $productFile[$i]->save(); } $productFile[1]->productFileGroup->set($productGroup[2]); $productFile[1]->save(); $this->assertEqual($this->product->getFileGroups()->getTotalRecordCount(), 3); $this->assertEqual($this->product->getFiles()->getTotalRecordCount(), 2); $filesMergedWithGroups = $this->product->getFilesMergedWithGroupsArray(); $this->assertEqual(count($filesMergedWithGroups), 4); // Check files without group $this->assertTrue(isset($filesMergedWithGroups[1]['ID'])); $this->assertEqual($filesMergedWithGroups[1]['ID'], $productFile[2]->getID()); $this->assertFalse(isset($filesMergedWithGroups[1]['ProductFileGroup']['ID'])); // Check first group $this->assertFalse(isset($filesMergedWithGroups[2]['ID'])); $this->assertTrue(isset($filesMergedWithGroups[2]['ProductFileGroup']['ID'])); $this->assertEqual($filesMergedWithGroups[2]['ProductFileGroup']['ID'], $productGroup[1]->getID()); // Check second group $this->assertTrue(isset($filesMergedWithGroups[3]['ID'])); $this->assertEqual($filesMergedWithGroups[3]['ID'], $productFile[1]->getID()); $this->assertTrue(isset($filesMergedWithGroups[3]['ProductFileGroup']['ID'])); $this->assertEqual($filesMergedWithGroups[3]['ProductFileGroup']['ID'], $productGroup[2]->getID()); // Check second group $this->assertFalse(isset($filesMergedWithGroups[4]['ID'])); $this->assertTrue(isset($filesMergedWithGroups[4]['ProductFileGroup']['ID'])); $this->assertEqual($filesMergedWithGroups[4]['ProductFileGroup']['ID'], $productGroup[3]->getID()); foreach ($productFiles as $fileName) { unlink($fileName); } foreach ($productFile as $file) { $file->delete(); } }
public function testChangeUploadedFile() { $productFile = ProductFile::getNewInstance($this->product, $this->tmpFilePath, 'some_file.txt'); $productFile->save(); $this->assertEqual(file_get_contents($productFile->getPath()), $this->fileBody); $reuploadedFile = ClassLoader::getRealPath('cache.') . 'reuploaded_file.txt'; file_put_contents($reuploadedFile, $reuploadedFileBody = 'Reupload file'); $productFile->storeFile($reuploadedFile, 'some_file.txt'); $productFile->save(); $this->assertEqual(file_get_contents($productFile->getPath()), $reuploadedFileBody); unlink($reuploadedFile); $productFile->delete(); }