public function testSave() { $manager = \Aimeos\MShop\Factory::createManager($this->context, 'product'); $item = $manager->findItem('CNC'); $item->setCode('jqadm-test-image'); $item->setId(null); $manager->saveItem($item); $param = array('image' => array('product.lists.id' => array(''), 'media.languageid' => array('de'), 'media.label' => array('test'))); $helper = new \Aimeos\MW\View\Helper\Param\Standard($this->view, $param); $this->view->addHelper('param', $helper); $files = array('image' => array('files' => array(array('tmp_name' => '', 'name' => '', 'type' => '', 'size' => 0, 'error' => 0)))); $helper = $this->getMockBuilder('\\Aimeos\\MW\\View\\Helper\\Request\\Standard')->setConstructorArgs(array($this->view, '', '', null, $files))->setMethods(array('checkUploadedFile'))->getMock(); $this->view->addHelper('request', $helper); $this->view->item = $item; $name = 'AdminJQAdmProductImageSave'; $this->context->getConfig()->set('controller/common/media/name', $name); $cntlStub = $this->getMockBuilder('\\Aimeos\\Controller\\Common\\Media\\Standard')->setConstructorArgs(array($this->context))->setMethods(array('add'))->getMock(); \Aimeos\Controller\Common\Media\Factory::injectController('\\Aimeos\\Controller\\Common\\Media\\' . $name, $cntlStub); $cntlStub->expects($this->once())->method('add'); $mediaStub = $this->getMockBuilder('\\Aimeos\\MShop\\Media\\Manager\\Standard')->setConstructorArgs(array($this->context))->setMethods(array('saveItem'))->getMock(); \Aimeos\MShop\Factory::injectManager($this->context, 'media', $mediaStub); $mediaStub->expects($this->once())->method('saveItem'); \Aimeos\MShop\Factory::setCache(true); $result = $this->object->save(); \Aimeos\MShop\Factory::setCache(false); $item = $manager->getItem($item->getId(), array('media')); $manager->deleteItem($item->getId()); $this->assertNull($this->view->get('errors')); $this->assertNull($result); $this->assertEquals(1, count($item->getListItems('media'))); }
public function testUploadItem() { $file = array('name' => 'test-binary.bin', 'tmp_name' => 'test.bin', 'error' => UPLOAD_ERR_OK, 'type' => 'application/binary', 'size' => 1024); $object = $this->getMockBuilder('\\Aimeos\\Controller\\ExtJS\\Media\\Standard')->setMethods(array('getUploadedFile'))->setConstructorArgs(array($this->context))->getMock(); $name = 'ControllerCommonMediaUploadItem'; $this->context->getConfig()->set('controller/common/media/name', $name); $stub = $this->getMockBuilder('\\Aimeos\\Controller\\Common\\Media\\Standard')->setMethods(array('add'))->setConstructorArgs(array($this->context))->getMock(); \Aimeos\Controller\Common\Media\Factory::injectController('\\Aimeos\\Controller\\Common\\Media\\' . $name, $stub); $stub->expects($this->once())->method('add'); $object->expects($this->once())->method('getUploadedFile')->will($this->returnValue($file)); $media = $object->uploadItem((object) array('site' => 'unittest', 'domain' => 'product')); \Aimeos\Controller\Common\Media\Factory::injectController('\\Aimeos\\Controller\\Common\\Media\\' . $name, null); $this->assertInstanceOf('\\stdClass', $media); }
public function testUploadItem() { $file = $this->getMock('\\Psr\\Http\\Message\\UploadedFileInterface'); $object = $this->getMockBuilder('\\Aimeos\\Controller\\ExtJS\\Media\\Standard')->setMethods(array('getUploadedFile'))->setConstructorArgs(array($this->context))->getMock(); $object->expects($this->once())->method('getUploadedFile')->will($this->returnValue($file)); $name = 'ControllerCommonMediaUploadItem'; $this->context->getConfig()->set('controller/common/media/name', $name); $stub = $this->getMockBuilder('\\Aimeos\\Controller\\Common\\Media\\Standard')->setMethods(array('add'))->setConstructorArgs(array($this->context))->getMock(); $stub->expects($this->once())->method('add'); \Aimeos\Controller\Common\Media\Factory::injectController('\\Aimeos\\Controller\\Common\\Media\\' . $name, $stub); $media = $object->uploadItem((object) array('site' => 'unittest', 'domain' => 'product')); \Aimeos\Controller\Common\Media\Factory::injectController('\\Aimeos\\Controller\\Common\\Media\\' . $name, null); $this->assertInstanceOf('\\stdClass', $media); }
/** * Stores an uploaded file * * @param \stdClass $params Associative list of parameters * @return \stdClass Object with success value * @throws \Aimeos\Controller\ExtJS\Exception If an error occurs */ public function uploadItem(\stdClass $params) { $this->checkParams($params, array('site', 'domain')); $this->setLocale($params->site); $context = $this->getContext(); $manager = \Aimeos\MShop\Factory::createManager($context, 'media'); $typeManager = \Aimeos\MShop\Factory::createManager($context, 'media/type'); $item = $manager->createItem(); $item->setTypeId($typeManager->findItem('default', array(), 'product')->getId()); $item->setDomain('product'); $item->setStatus(1); $file = $this->getUploadedFile(); \Aimeos\Controller\Common\Media\Factory::createController($context)->add($item, $file); $manager->saveItem($item); return (object) $item->toArray(); }
/** * Updates existing product image references or creates new ones * * @param \Aimeos\MW\View\Iface $view View object with helpers and assigned parameters */ protected function updateItems(\Aimeos\MW\View\Iface $view) { $id = $view->item->getId(); $context = $this->getContext(); $manager = \Aimeos\MShop\Factory::createManager($context, 'product'); $mediaManager = \Aimeos\MShop\Factory::createManager($context, 'media'); $listManager = \Aimeos\MShop\Factory::createManager($context, 'product/lists'); $cntl = \Aimeos\Controller\Common\Media\Factory::createController($context); $listIds = (array) $view->param('image/product.lists.id', array()); $listItems = $manager->getItem($id, array('media'))->getListItems('media', 'default'); $mediaItem = $this->createItem(); $listItem = $this->createListItem($id); $files = $view->value($view->request()->getUploadedFiles(), 'image/files', array()); $num = 0; foreach ($listIds as $idx => $listid) { if (!isset($listItems[$listid])) { $litem = $listItem; $litem->setId(null); if (($file = $view->value($files, $num)) !== null) { $item = $mediaItem; $item->setId(null); $cntl->add($item, $file); $num++; } else { throw new \Aimeos\Admin\JQAdm\Exception(sprintf('No file uploaded for %1$d. new image', $num + 1)); } } else { $litem = $listItems[$listid]; $item = $litem->getRefItem(); } $item->setLabel($view->param('image/media.label/' . $idx)); $item->setLanguageId($view->param('image/media.languageid/' . $idx)); $mediaManager->saveItem($item); $litem->setPosition($idx); $litem->setRefId($item->getId()); $listManager->saveItem($litem, false); } $this->cleanupItems($listItems, $listIds); }
public function testCreateControllerNotExisting() { $this->setExpectedException('\\Aimeos\\Controller\\Common\\Exception'); \Aimeos\Controller\Common\Media\Factory::createController(\TestHelperCntl::getContext(), 'notexist'); }
/** * Updates existing product image references or creates new ones * * @param \Aimeos\MW\View\Iface $view View object with helpers and assigned parameters */ protected function updateItems(\Aimeos\MW\View\Iface $view) { $id = $view->item->getId(); $context = $this->getContext(); $manager = \Aimeos\MShop\Factory::createManager($context, 'product'); $mediaManager = \Aimeos\MShop\Factory::createManager($context, 'media'); $listManager = \Aimeos\MShop\Factory::createManager($context, 'product/lists'); $mediaTypeManager = \Aimeos\MShop\Factory::createManager($context, 'media/type'); $listTypeManager = \Aimeos\MShop\Factory::createManager($context, 'product/lists/type'); $cntl = \Aimeos\Controller\Common\Media\Factory::createController($context); $listIds = (array) $view->param('image/product.lists.id', array()); $listItems = $manager->getItem($id, array('media'))->getListItems('media'); $listItem = $listManager->createItem(); $listItem->setTypeId($listTypeManager->findItem('default', array(), 'media')->getId()); $listItem->setDomain('media'); $listItem->setParentId($id); $listItem->setStatus(1); $mediaItem = $mediaManager->createItem(); $mediaItem->setTypeId($mediaTypeManager->findItem('default', array(), 'product')->getId()); $mediaItem->setDomain('product'); $mediaItem->setStatus(1); $files = $view->value($view->request()->getUploadedFiles(), 'image/files', array()); $files = is_array($files) ? $files : array($files); $num = 0; foreach ($listIds as $idx => $listid) { if (!isset($listItems[$listid])) { $litem = $listItem; $litem->setId(null); if (($file = $view->value($files, $num)) === null) { throw new \Aimeos\Admin\JQAdm\Exception(sprintf('No uploaded file for %1$d. new entry ', $num)); } $cntl->add($mediaItem, $file); $num++; } else { $litem = $listItems[$listid]; $item = $litem->getRefItem(); } $mediaItem->setLabel($view->param('image/media.label/' . $idx)); $mediaItem->setLanguageId($view->param('image/media.languageid/' . $idx)); $mediaManager->saveItem($mediaItem); $litem->setPosition($idx); $litem->setRefId($mediaItem->getId()); $listManager->saveItem($litem, false); } $rmIds = array(); $rmListIds = array_diff(array_keys($listItems), $listIds); foreach ($rmListIds as $rmListId) { $item = $listItems[$rmListId]->getRefItem(); $cntl->delete($item); $rmIds[] = $item->getId(); } $listManager->deleteItems($rmListIds); $mediaManager->deleteItems($rmIds); }