I have a Symfony2 entity called MyUploadedFiles and a form type MyUploadedFilesType with the name myuploadedfiles. This form type builds a form with a single field file. This field should not be blank and the rest of the data in the entity is generated, once a user uploads a file. Now, I want to run a functional test for my controller that handles the upload. But I I always get the error file should not be blank.
This is the code in my myUploadTest method:
$filepath = __DIR__.'/test_file.xml';
$xmlfile = new UploadedFile(
$filepath,
'foo.xml',
'text/xml',
\filesize($filepath)
);
$client->request(
'POST',
'/path/to/xml_upload',
array(),
array('myuploadedfiles[file]' => $xmlfile)
);
$response = $client->getResponse();
$this->assertEquals(Codes::HTTP_CREATED, $response->getStatusCode());
The controller looks like:
$form->bind($request);
if ($form->isValid()) { ... }
If I render the form with a twig template and try to upload something everything works fine. So probably the error is somewhere in the test code.
Thanks for the help!
I use this code:
# Select the file from the filesystem
# Path of the file to send
$filepath = __DIR__.'/test_file.xml';
$xmlfile = new UploadedFile(
$filepath,
'foo.xml',
'text/xml',
\filesize($filepath)
);
# Select the form (adapt it for your needs)
$form = $crawler->filter('input[type=submit]...')->form();
# Put the file in the upload field
$form['myuploadedfiles[file]']->upload($xmlfile);
# Send it
$crawler = $this->client->submit($form);
# Check that the file has been successfully sent
# (in my case the filename is displayed in a <a> link so I check
# that it appears on the page)
$this->assertEquals(
1,
$crawler->filter('a:contains("foo.xml")')->count()
);
Related
I have a Leave application project that generates a PDF file.
this is the code that generates the pdf after I click submit.
$id = DB::table('leave')->where('user_id', auth()->user()->id)->max('id');
$data = leave::where('id', $id)->select('*')->get();
$filename = auth()->user()->name." - S - ". "LEAVE - ".$randomString->randomString(8)."-".$request->filing_date.".pdf";
$path = storage_path('app/leave_file/'.$filename);
PDF::loadView('pdf_views.leave_pdf', $data[0])->setPaper('legal')->save($path);
if(file_exists($path)){
DB::table('leave_files')->insert([
'filename' => $filename,
'leave_id' => DB::table('leave')->where('user_id', auth()->user()->id)->max('id'),
]);
}
return redirect()->route('leave')->with('success', "success");
I wonder if I can make the newly generated pdf open in the new tab after I click submit on my leave application. thank you to anyone who can help
According to the Laravel docs this is what you need: https://laravel.com/docs/9.x/responses#file-responses
File Responses
The file method may be used to display a file, such as an image or PDF, directly in the user's browser instead of initiating a download. This method accepts the path to the file as its first argument and an array of headers as its second argument:
return response()->file($pathToFile);
return response()->file($pathToFile, $headers);
Use the Cache Facade, to store the generated pdf temporary.
$uuid = Str::uuid();
return response()->stream(function () use ($uuid) {
echo Cache::remember($uuid, 300, function() {
return 'genertated pdf content'
});
}, 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => sprintf('attachment; filename=%s;', $uuid),
]);
Create a route to access to cached file. Name the route.
Now you can open it in a new tab from you frontend e.g. Generate the route with route('routename', ['param' => 123]) and pass it to your FE with a REST endpoint for example.
window.open(url,'_blank');
I have created a plugin and after the other processes of the plugin are done i would like to redirect to a given url from a controller in the backend.
i have created a plugin that creates a documents from orders and that is working fine. however at the end of the process i would like to redirect to a url that can download or open the document that has been created. i know the url for doing so is structured like this (http://localhost:8000/backend/Order/openPdf?id=harshvalueforpdf). i am using shopware version 5.5.1 in docker on my local host.
public function redirectmyurlAction()
{
$harsh = "9ce6b9a9cd5d469386fbb5bd692f9644";
$search_word = $harsh;
error_log(print_r(array('Reached redirect action'), true)."\n", 3, Shopware()->DocPath() . '/test.log');
$this->redirect(
array(
'module'=> backend,
'controller' => 'Order',
'action' => 'openPdf?id='.$search_word,
)
);
}
i expect that when the process reached this action the user is redirected to the created url and then it should be able to download or show the pdf. But it logs the log i put before the redirect but does not redirect. nothing is logged in errors or console. When i put the same redirect on the frontend i get the CSRFTokenValidationException which is what i expect, but it shows the redirect works there so why not in the backend.
Update:
After the responses,i have copied the function and modified it as below but it logs everything there and still does nothing am i missing something?
public function openmyPdf($DocHarsh, $orderId)
{
error_log(print_r(array('Entered openmyPdf function',$DocHarsh,$orderId,$date), true)."\n", 3, Shopware()->DocPath() . '/error.log');
$filesystem = $this->container->get('shopware.filesystem.private');
$file = sprintf('documents/%s.pdf', basename($DocHarsh));
if ($filesystem->has($file) === false) {
error_log(print_r(array('Entered if statement, file doesnt exists ',$DocHarsh,$orderId,$date), true)."\n", 3, Shopware()->DocPath() . '/error.log');
$this->View()->assign([
'success' => false,
'data' => $this->Request()->getParams(),
'message' => 'File not exist',
]);
return;
}
// Disable Smarty rendering
$this->Front()->Plugins()->ViewRenderer()->setNoRender();
$this->Front()->Plugins()->Json()->setRenderer(false);
$orderModel = Shopware()->Models()->getRepository(Document::class)->findBy(['hash' =>$DocHarsh]);
$orderModel = Shopware()->Models()->toArray($orderModel);
$orderId = $orderModel[0]['documentId'];
$response = $this->Response();
$response->setHeader('Cache-Control', 'public');
$response->setHeader('Content-Description', 'File Transfer');
$response->setHeader('Content-disposition', 'attachment; filename=' . $orderId . '.pdf');
$response->setHeader('Content-Type', 'application/pdf');
$response->setHeader('Content-Transfer-Encoding', 'binary');
$response->setHeader('Content-Length', $filesystem->getSize($file));
$response->sendHeaders();
$response->sendResponse();
$upstream = $filesystem->readStream($file);
$downstream = fopen('php://output', 'wb');
while (!feof($upstream)) {
fwrite($downstream, fread($upstream, 4096));
}
error_log(print_r(array('leaving the pdf function',$DocHarsh,$orderId,$upstream,$downstream), true)."\n", 3, Shopware()->DocPath() . '/error.log');
}
Please have a look at backend-controller of the order module. It should be the same case. This function is used for opening/downloading a document from the backend:
https://github.com/shopware/shopware/blob/5.5/engine/Shopware/Controllers/Backend/Order.php#L1113
I think it might be confusing for backend users to be redirected (from the backend context) to a new blank page with a download.
According to my own evaluation. I think the issue you are having is because this is not an action but simply a function try making it an Action and run it through the browser like the original one.
Don't forget to whitelist it.
use the class use Shopware\Components\CSRFWhitelistAware;
then
something like this
/**
* {#inheritdoc}
*/
public function getWhitelistedCSRFActions()
{
return [
'youropenPdfActionnamewithoutthewordAction'
];
}
and also add the implements CSRFWhitelistAware to your class declaration.
I created a function in PHP that downloads a map created in google maps, I am able to download, however, the route is not coming when I am saving.
How the picture is coming:
How it should come:
Code:
function download_remote_file_with_curl($file_url, $save_to) {
$arrContextOptions = array(
"ssl" => array(
"verify_peer" => false,
"verify_peer_name" => false,
),
);
$response = file_get_contents($file_url, false, stream_context_create($arrContextOptions));
//echo $response;
//file_put_contents($save_to, $response);
$downloaded_file = fopen($save_to, 'wb');
fwrite($downloaded_file, $response);
fclose($downloaded_file);
}
$urlDownload = "https://maps.googleapis.com/maps/api/staticmap?size=1000x1000&maptype=roadmap&path=enc:zklaAbqcyJzCmF~#b#fBdAzCiEnKyQlFpCnMdH~_#nU~JlFlCNrw#oL~IcApEZxd#nQpgBhq#tRtStgA``Aty#`o#ll#pa#tNhFzMvJfG~D|Y~Hxd#rMll#`Rtb#jLjNzExWtFz`#bJvM|ItG`LdF|V`GvD|CL|JiBv]aGld#cHrk#cLdU_Fpb#wGdMg#vaAqAti#g#z_#Cj~A{#raAfDlp#xAfYa#tyAuDtVEbTdApO`#rGk#dP`#vgEo#baCuA`|AaArHGIzAwCtHqDlNf#rl#lA~bAnAphAMj`#hBlj#\hd#s#tL\pHnCff#~A`|#lMnmEnBbKrFbDn`#jP~RtQ~k#hVhNtGb#xFgPheAsAnOuIlaBpj#|qCtDzGb`A~`AzDvGd#vI{Af_#x#~GxItHzLvGp\fLxc#t\pe#d`#h|AnmA`WvQnEDnF}Bd\uUjGeB~Gb#nEvB~KnMri#ni#fjCbfCpmArkAv|#pz#bjAjhApcEt}Ddx#pv#r_#lZzyB|hBtoHneGzZvVh|#xq#fb#jZh^fZjD~E`FlOzMbc#|L|`#hQrS~H`MhQ~w#hEdzB~B~hAbAbk#Ux^kNx~BeGriAaCbe#|#zSt#`ChDnBnZlAlFtCl`#`AzMpBpRvJlKvFbKlLxd#pcAdk#~oAjbAbiAnPxLfq#b^ndDpkBbdDfjBbqAdt#hFjAtNhCfMbD|g#tYhxDfxBrqFx~C|CpCzA|EE~E}i#raDsOb}#uO~{#{f#zvCr#hG~CdDf`Bly#hL|Lt#bClD|Brx#naAvAnAzb#lg#zj#veDjMbI|rBhdAlJzFdAjCbBpgBpMlsQdIhhO]p^?hb#z#dsAp#jo#l#pWffDp`FzyB~eDxdEhlGpNrSvv#hn#duEvuDnsJp~HtpLvvJ~bBtvAjw#hp#hDnFnHvpBlSh}FrFd_BvAno#tu#buTtPxwEnXvhJbRddGbQ~nGlNjqFlJv~BvC~rAdFppBvNhvEhShsGw#nQ}i#htBsq#lgCcJd]}BjPkG|y#gPptBu]byE_H~gArB`EfG|FxXnYfv#nv#nWhX`RjRjHj`#bfBdgKlCrKzGhQp`#lbAtdBnjEr_#z`A`~#n}Bth#fuAdpDzdJ|W`o#`ClAxIj#beA~A~p#bAx|F~I`lEzGvwAbCdMx#neDjlAbp#hUrE}#prB_qCzBaAjJAxOKhWfAfP?tm#}Q~{Bkr#r{EeyAxPoApfB~WzK|AHTdBDrO}A~RmBrXsCzZuA~NJx#bD|#dAfX`#{#nG&markers=color:green|label:A|-10.8819552,-61.95483539999998&markers=color:red|label:B|-12.457280801085,-64.2310285267651&key=yourkey";
download_remote_file_with_curl($urlDownload, realpath('C:/test') . '/' . 'test' . '.jpg');
?>
Thank you!
I don't know if what I'm about to write makes sense, but the route that you want to download is an item that appears once it's selected.
For example when the "SKIP" buttom in Youtube appears to quit an advertisement, it creates an instance of that box in HTML, and then it dissappears when Add is over.
I think that the route is this kind of instance and you must add a line of code to also take that piece of html
I'm using HTTP client. How to attach a file while sending a post request?
Here's my code:
function executePost($postdata = [], $full_file_path = ''){
self::$client = new Zend\Http\Client(null,
['timeout' => 30] // updated to 30 sec
);
self::$client->setEncType(Zend\Http\Client::ENC_URLENCODED);
self::$client->setUri($url);
self::$client->setMethod($method);
self::$client->setParameterPost($postdata);
$response = $client->send();
Debug::dump($response->getContent(),$label='Response',$echo=true);
}
I have tried $postdata['file_contents'] = '#'.$full_file_path; but it doesn't help.
Does anyone have an idea on how to attach file with the post data?
Thanks in advance.
Instead of using that data array try using the designated method called setFileUpload. So in your example it could look like so:
$client = new \Zend\Http\Client(null, ['timeout' => 30]);
$client->setUri($url);
$client->setFileUpload($full_file_path, 'file_contents');
$client->setMethod('POST'); // this must be either POST or PUT
$response = $client->send();
setFileUpload should also set the enc type.
There's a doc section related to that here.
I've created a Console route on my zf2 application to dispatch an email via the command line. So in my action I am creating a new PhpRenderer as specified in the documentation (http://framework.zend.com/manual/2.0/en/modules/zend.view.renderer.php-renderer.html) to render my email template (replace variables etc) and dispatch it.
Here is the code I am using:
$renderer = new \Zend\View\Renderer\PhpRenderer();
$resolver = new \Zend\View\Resolver\TemplateMapResolver();
$resolver->setMap(array(
'mailTemplate' => $config['template']
));
$renderer->setResolver($resolver);
$model = new \Zend\View\Model\ViewModel();
$model->setTemplate('mailTemplate');
$model->setVariables(array(
'recipient' => 'foo#bar.com'
));
$emailBody = $renderer->render($model);
However it seems that the Phprenderer does not render any PHP in my .phtml file. The exact same code works correctly however if I execute it in a normal HTTP Request.
Could anyone help me out with this?
I suspect that it's because you haven't set the correct path to the resolver.
For reference, this is the approach I took:
// render HTML and TEXT bodies
$basePath = realpath(__DIR__ . '/../../../view/emails');
$htmlFilename = 'body.html.phtml';
$txtFilename = 'body.txt.phtml';
$renderer = new PhpRenderer();
$renderer->resolver()->addPath($basePath);
$sm = $this->getServiceManager();
$renderer->setHelperPluginManager($sm->get('ViewHelperManager'));
$model = new ViewModel();
$model->setVariable('name', $user->getName());
$model->setTemplate($txtFilename);
$textContent = $renderer->render($model);
$model->setTemplate($htmlFilename);
$htmlContent = $renderer->render($model);