how to write Feature Test for -excel document upload check - laravel

i'm using https://docs.laravel-excel.com/ for uploading Excel and then display on view
here is my controller
public function import()
{
$rows= Excel::toArray(new ProfitAndLosImport, request()->file('pl'));
return view("pl")->with(compact('rows'));
}
how to write test for checking this process ?
Any help would be much appreciated
i'm using this but it's working
public function testUpload()
{
Excel::fake();
$this->actingAs(factory('App\User')->create());
$testFile = UploadedFile::fake()->create('borrowers.xlsx');
$response =$this->post('pl',['pl'=>$testFile]);
Excel::assertImported('create_terms.xls', function(ProfitAndLosImport $import) {
return true;
});
}

Related

Is it possible to retrieve a value from a redirect in another class before performing the redirect?

I am currently working with a controller, OnboardingController.php. This controller calls another method in a different class, let's call it OnboardingService.php, so for example:
OnboardingController
public function doThing()
{
return $this->doAnotherThing();
}
OnboardingService
public function doAnotherThing()
{
return redirect('/')->with(['propertyA' => 'valueA']);
}
Would I be able to access propertyA in OnboardingController before returning the redirect? And if so, how would I access that property?
e.g.
OnboardingController
public function doThing()
{
$doAnotherThing = $this->doAnotherThing();
Log::info($doAnotherThing->propertyA);
return $doAnotherThing;
}
I am currently using Laravel 6.
You're using redirect(...)->with(...) to flash propertyA, so propertyA should be accessible from the session
public function doThing()
{
$doAnotherThing = $this->doAnotherThing();
Log::info(session()->get('propertyA'));
return $doAnotherThing;
}

Unable to trigger event(post.customer.login and post.customer.logout) in Opencart 3.0.2.0

I want to set the session after user login in opencart-3.0.2.0
I am new to opencart, I have just created this two files only in the corresponding folder.anything else I need to be done to trigger the event.
I am referring this link to trigger the event in opencart:https://isenselabs.com/posts/opencart2-event-system-tutorial
I have searched a lot on google still no result found.
Code that I am using to trigger event in opencart.
path : admin/controller/module/mymodule.php
Code :
public function install() {
$this->load->model('extension/event');
$this->model_extension_event->addEvent('mymodule', 'pre.admin.store.delete', 'module/mymodule/on_store_delete');
$this->model_extension_event->addEvent('mymodule', 'post.customer.login', 'module/mymodule/post_customer_login_customtoken');
$this->model_extension_event->addEvent('mymodule', 'post.customer.logout', 'module/mymodule/post_customer_logout_function');
}
public function uninstall() {
$this->load->model('extension/event');
$this->model_extension_event->deleteEvent('mymodule');
}
public function on_store_delete($store_id) {
$this->load->model('setting/store');
$store_info = $this->model_setting_store->getStore($store_id);
$admin_mail = $this->config->get('config_email');
mail($admin_mail, "A store has been deleted", "The store " . $store_info['url'] . " was deleted.");
}
}
path : catalog/controller/module/mymodule.php
Code :
<?php
class ControllerModuleMyModule extends Controller {
public function post_customer_login_customtoken() {
$str = 'abcdefghigklmnopqrstuvwxyz';
$shuffled = str_shuffle($str);
$this->session->data['custom_token'] = $shuffled;
}
public function post_customer_logout_function(){
$this->log->write("post_customer_logout_function");
unset($this->session->data['custom_token']);
}
}
That tutorial is for OpenCart 2.0 - 2.1, in OpenCart 2.2 and above Event system has been changed.
For OpenCart 3.0.2.0 instead of:
$this->load->model('extension/event');
// and
$this->model_extension_event->addEvent
use:
$this->load->model('setting/event');
// and
$this->model_setting_event->addEvent
Instead of:
'post.customer.login'
use:
'catalog/controller/account/login/after'
Instead of:
deleteEvent
Use:
deleteEventByCode
So it must be:
admin\controller\extension\module\mymodule.php
public function install(){
$this->load->model('setting/event');
$this->model_setting_event->addEvent('mymodule', 'catalog/controller/account/login/after', 'extension/module/mymodule/after_customer_login_customtoken');
}
public function uninstall(){
$this->load->model('setting/event');
$this->model_setting_event->deleteEventByCode('mymodule');
}
catalog\controller\extension\module\mymodule.php
class ControllerExtensionModuleMyModule extends Controller {
public function after_customer_login_customtoken(){
$this->log->write('test');
}
}

one controller for three pages

I have one controller ADD. I want this controller to manipulate three pages: add_customer page, add_project page and add_post_page.In this case add_project page works perfectly. But add_customer and add_post pages have errors:
Failed to load resource: the server responded with a status of 500 (Internal Server Error).
I think the problem is in my coditions here:
public function index(){
if($this->uri->segment(3)=='add_customer'){
$this->add_customer();
}
else if($this->uri->segment(3)=='add_post'){
$this->add_post();
}
else{
$this->add_project();
}
}
This is my full controller:
class ADD extends MX_Controller {
public $mname, $tag, $tpl;
function __construct()
{
$this->mname=strtolower(get_class());
$this->tag=strtoupper($this->mname);
}
public function index(){
if($this->uri->segment(3)=='add_customer'){
$this->add_customer();
}
else if($this->uri->segment(3)=='add_post'){
$this->add_post();
}
else{
$this->add_project();
}
}
public function add_project()
{
include APPPATH."language/".LANG.".php";
$this->load->model($this->mname.'/add_project_model');
$model='add_project_model';
$this->$model->index($this->mname);
$a['IsEnabled']=$LANGUAGE['IsEnabled'];
$a['Submit']=$LANGUAGE['Submit'];
$a['Cancel']=$LANGUAGE['Cancel'];
$a['Reset']=$LANGUAGE['Reset'];
$a['Name']=$LANGUAGE['Name'];
$a['SelectCustomer']=$LANGUAGE['SelectCustomer'];
$a['Project Name']=$LANGUAGE['ProjectName'];
$a['Manager']=$LANGUAGE['Manager'];
$a['Customer']=$LANGUAGE['Customer'];
$userGROUP = $this->session->userdata('_userGROUP');
if ($userGROUP=='Administrator')
$a['AddManager']='<button type="button" class="btn btn-warning" onclick="AddNewManager()">+</button>';
else
$a['AddManager']='';
$this->tp->assign($a);
$this->tp->parse('CONTENT', $this->mname.'/add_project.tpl');
}
public function add_customer()
{
include APPPATH."language/".LANG.".php";
$userGROUP = $this->session->userdata('_userGROUP');
if($userGROUP!=='Administrator')
{
show_404('page');
exit;
}
$this->load->model($this->mname.'/add_customer_model');
$model='add_customer_model';
$this->$model->index($this->mname);
$a['IsEnabled']=$LANGUAGE['IsEnabled'];
$a['Submit']=$LANGUAGE['Submit'];
$a['Cancel']=$LANGUAGE['Cancel'];
$a['Reset']=$LANGUAGE['Reset'];
$a['Name']=$LANGUAGE['Name'];
$a['Project Name']=$LANGUAGE['CustomerName'];
$a['Customer Name']=$LANGUAGE['Customer Name'];
$this->tp->assign($a);
$this->tp->parse('CONTENT', $this->mname.'/add_customer.tpl');
}
public function add_post()
{
include APPPATH."language/".LANG.".php";
$userGROUP = $this->session->userdata('_userGROUP');
if($userGROUP=='Engineer')
{
show_404('page');
exit;
}
$this->load->model($this->mname.'/add_post_model');
$model='add_post_model';
$this->$model->index($this->mname);
$a['IsEnabled']=$LANGUAGE['IsEnabled'];
$a['Submit']=$LANGUAGE['Submit'];
$a['Cancel']=$LANGUAGE['Cancel'];
$a['Reset']=$LANGUAGE['Reset'];
$a['Activity Name']=$LANGUAGE['Activity Name'];
$a['SalaryHour']=$LANGUAGE['SalaryHour'];
$this->tp->assign($a);
$this->tp->parse('CONTENT', $this->mname.'/add_post.tpl');
}
}
How can I resolve this problem?
Just Give it a Try, Hope it works :
public function index(){
if($this->uri->segment(3)=='add_customer'){
redirect(base_url().'/Add/add_customer');
//$this->add_customer();
}
else if($this->uri->segment(3)=='add_post'){
redirect(base_url().'/Add/add_post');
//$this->add_post();
}
else{
redirect(base_url().'/Add/add_project');
//$this->add_project();
}
}
Your index function is never reached
This would reach it : http://project.dev/add
By default the following urls hit the appropriate function.
http://project.dev/add/add_customer hit public function add_customer()
http://project.dev/add/add_post hit public function add_post()
If you are getting errors check out the various error in the specific function and make sure you have error reporting turned on

Is there a way to send CakeResponse from blackhole callback

I am trying to make the ajax request more user friendly. It is a scenario that using a token which can be reuse in a period of time. However in time that a user has inactive for a certain time and tried to use the form, user will observe that the ajax not working but donno why. What I'm gonna do here is to display the message.
Using my testing code, doing return new CakeResponse seems to be return true in blackhole and therefore the $result is true although the User edit should not be triggered
public function beforeFilter() {
$this->Security->blackHoleCallback = 'blackhole';
}
public function blackhole($type) {
if ($this->request->is('ajax')) {
$this->log('blackhole here','debug');
return new CakeResponse(array('body'=> json_encode(array('data'=>'The request has been blackholed')),'status'=>500));
}else{
$this->log('blackhole there','debug');
return new CakeResponse(array('body'=> json_encode(array('data'=>'The request has been blackholed')),'status'=>500));
}
}
in the userapp
public function edit() {
$userId=$this->Auth->user('id');
if (empty($this->request->data)) {
$this->request->data = $this->User->read(null, $userId);
}
if ($this->request->is('ajax')) {
if($result = $this->{$this->modelClass}->edit($userId, $this->request->data)){
$this->Session->write('Auth', $this->User->read(null, $userId));
return new CakeResponse(array('body'=> json_encode(array('data'=>'saved')),'status'=>200));
}else{
return new CakeResponse(array('body'=> json_encode(array('data'=>'error')),'status'=>500));
}
}
}
Progress 1:
Unsolved, perhaps needed to use event handler or exception, though it will be somehow complicated . Still thinking while refining other plugin feature.

CIUnit Testing foostack

Is there anyone here used CIUnit?
Having trouble understanding it. T_T.
What I'm doing is pretty simple
E.g
I have a controller method myphp()
function myphp()
{
echo 'boom';
}
CI Unit Testing:
public function setUp()
{
// Set the tested controller
$this->CI = set_controller('home');
// date_default_timezone_set('America/Los_Angeles');
}
function testMyPhp()
{
$this->CI->myphp();
$out = output();
var_dump($out); //return empty eventhough function myphp is returning a string ('boom')
}
What happening.?? I just want to test whether I can fetch the output of my method myphp.

Resources