I am tring to make test for my project, could someone show me example how to write tests or give me some good video course to learn testing.
In my example I am tring to test this part:
public function getProjectsForUser(){
if(Auth::user() -> admin_id == NULL){
$this->id = Auth::user() -> id;
}else{
$this->id = Auth::user() -> admin_id;
}
$projects = User::findOrFail(Auth::user() -> id)->projects()->where('admin_id', '=', $this->id)->get();
$role = User::findOrFail(Auth::user() -> id)->roles;
$users = User::where('admin_id', '=', $this->id)->lists('name','id');
return array('projects' => $projects,'users' => $users,'roles' => $role );
}
It was my model Project.php
Here is my PorojectController:
public function project(Project $projects){
$this -> projects = $projects ->getProjectsForUser();
return view('projects',$this -> projects);
}
Here is my test to check if user logged...
public function testHome()
{
$this->be(User::find(1));
$response = $this->call('GET', '/projects');
//CHECK IF AUTH USER
$this->assertTrue($response->isOk());
}
Now I need to check if I get right values inside array, for $project, $role, $users.
You can use assertViewHas() to assert values that should have been passed to the view. For example:
public function testHome(){
$this->be(User::find(1));
$response = $this->call('GET', '/projects');
//CHECK IF AUTH USER
$this->assertTrue($response->isOk());
$this->assertViewHas('projects', 'expected value for projects');
$this->assertViewHas('users', 'expected value for users');
$this->assertViewHas('roles', 'expected value for roles');
}
By the way: If you pass no second argument the method will just assert that the variable exists but won't compare the value.
To accomplish the same with a bit a different syntax you can use assertViewHasAll() and pass everything as array:
$this->assertViewHasAll([
'projects' => 'expected value for projects',
'users' => 'expected value for users',
'roles' => 'expected value for roles'
]);
Related
i have this PATCH function but i need to add some form of authorization to ensure you can only edit/update a film that is associated with the current user, can i get some help on how to add this
controller function:
public function update(string $id)
{
$this->user = Auth::user();
$this->film = film:findOrFail($id);
return $this->film->toJson();
}
I've looked at the laravel docs at the validation section and seen this example
$validatedData = $request->validate([
'title' => 'required|unque:posts|max:255',
'body' => 'required',
]);
i then added my own validation at the top of the file
protected $validation = [
'name' => 'string',
'description' => 'new description'
];
im a little lost on how i implement authorization to ensure only a current user can update a film?
What you're looking for is not a form validation, but a User Authorization (as in the comments). So you should have a look at the official documentation. In your case you should write a FilmPolicy that may look like to this (I will skip the registration part... It can be easily understood from the docs):
class FilmPolicy {
/**
* Determine if the given film can be updated by the user.
*
* #param \App\User $user
* #param \App\Post $post
* #return bool
*/
public function update(User $user, Film $film)
{
return $user->id === $film->user_id; // Or whatever is your foreign key
}
}
Then you should update your controller in order to handle the authorization as follow:
public function update(string $id)
{
$this->film = film::findOrFail($id);
$this->authorize('update', $this->film);
return $this->film->toJson();
}
Since this method simply throws an exception, you can have a more elaborate response as explained in the docs
Ok basically, to enable what you need in a simple way, what you can do is this;
First pass the 'user_id' to the controller.
public function update(string $id, $userid)
{
$user = Auth::user();
$id = $user->id;
if($id == $userid)
{
$this->user = Auth::user();
$this->film = film::findOrFail($id);
return $this->film->toJson();
}else{
return "Not Authorized";
}
}
If im not misunderstanding your question, this basically allows only the user who is logged in to update his film. if he goes into any other profile, the id's would mismatch and thus return a not authorized prompt.
I learning about laravel framework, I don't know how to access property in my controller.
public function uuid(Request $request)
{
if($request->get('uuid') == null) return abort(404);
$uuid = $request->get('uuid');
$users = DB::table('users')->select('*')->where('uuid', $uuid)->get();
$result = array([
'id' => $users['id'],
'username' => $users['username'],
'uuid' => $users['uuid'],
]);
return view ('dashboard')->with('username', $result['username']);
}
in my dashboard.blade.php
{{$username}}
When i come to dashboard it show error like this
ErrorException (E_NOTICE)
Undefined index: username
Use First() instead of get() you'll get object so access data like.
$users = DB::table('users')->select('*')->where('uuid', $uuid)->first();
$result = array([
'id' => $users->id,
'username' => $users->username,
'uuid' => $users->uuid,
]);
return view ('dashboard')->with('username', $result['username']);
Now sort way to do it.
$user = DB::table('users')->select('*')->where('uuid', $uuid)->first();
$username = '';
if(!empty($user)){
$username = $user->username
}
return view ('dashboard',compact('username'));
$users is a collection of users. So you cannot access a property of a user like $users['id'];
If you want to get one user object from the database, you need to call first() instead of get()
Possible Solution
$user = DB::table('users')->select('*')->where('uuid', $uuid)->first();
You can use firstOrFail();
$users = DB::table('users')->select('*')->where('uuid', $uuid)->firstOrFail();
$result = array([
'id' => $users->id,
'username' => $users->username,
'uuid' => $users->uuid,
]);
return view ('dashboard')->with('username', compact('username'));
A possible short version of your solution may be like following
public function uuid(Request $request)
{
$user = User::select('username')->where('uuid', $request->uuid)->firstOrFail();
return view ('dashboard')->with('username', $user->username);
}
I am using the following code
public function show()
{
$id = Auth::user()->id;
$usuario = User::find($id);
$mascotin = Mascota::all();
$mascota = Mascota::find($id);
$mascota->save();
$cant_mascota = Mascota::count();
$cant_pregunta = Pregunta::count();
return view('usuario.show',[
'usuario' => $usuario,
'mascotin' => $mascotin,
'mascota' => $mascota,
'cant_mascota' => $cant_mascota,
'cant_pregunta' => $cant_pregunta,
]);
}
It gives me this error
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR).Call to a member function save() on null
User Model
public function mascotas(){
return $this->hasMany('App\Mascota','user_id', 'id');
}
Mascota Model
public function usuario()
{
return $this->belongsTo('App\User', 'id','user_id');
}
Route
Route::get('/home', 'UserController#show')->name('home');
Hope you guys can help me, I'm new in laravel and I have like 1 day tring to solve this problem
$usuario = Auth::user();
$id = $usuario->id; // you already have user from Auth or Request, does not need to request database again
$mascotin = Mascota::all();
$mascota = $mascotin->find($id); // you can search in collection
//if you want to create Mascotin if it doesn't exists use Mascota::firstOrCreate(['id' => $id]);
if(!$mascota){
throw new \Exception('Mascota not found', 404); //if $mascota is mandatory
}
$mascota->save(); // this does not have place here unless you are changing $mascota before that
$cant_mascota = $mascotin->count();
$cant_pregunta = Pregunta::count();
Also you should add auth middleware to this route. Only logged users should see it.
I am not sure what "Mascota" means (it will be good to use english when you share your code) but it is not good to have the same id as user. Better use relationships.
This is my code:
public function getLists(Request $request)
{
$user = $request->user()->id;
$apikey = DB::table('apikey')->where('api_key', '=', $user);
if($apikey){
$mc = new MailChimp($apikey);
$mailchimp_ping = $mc->get('lists',['fields' =>
'lists.id,lists.name']);
return Response::json($mailchimp_ping, 200);
}
else
{
$errorResponse = [
'message' => 'Lists not found!',
'error' => '401'
];
return Response::json( $errorResponse);
}
}
I am trying to get mailchimp list based on logged in user id where i am doing wrong? is my where clause expects something else?
Any help would be highly appreciated!
Use the value() method to execute the query and get the key. For example, if a column with key is called apikey:
$apikey = DB::table('apikey')->where('api_key', $user)->value('apikey');
In my case this error came up when changed this:
Route::view('/report', '/main_pages/orders_parts/report')->name('guzorishi_order_1');
to
Route::get('/report/{order_id}', function ($order_id) {... })->name('guzorishi_order_1');
but forgot Route::view one
rename to Route::get
I've seen alot of people using this way to check if a laravel model got saved. So now I wonder if it is a safe way.
And also can I check if the queries bellow got executed like this
Check if model got saved
Eg:
$myModel = new User();
$myModel->firstname = Input::get('firstname');
$myModel->lastname = Input::get('lastname');
$myModel->save();
//Check if user got saved
if ( ! $myModel->save())
{
App::abort(500, 'Error');
}
//User got saved show OK message
return Response::json(array('success' => true, 'user_added' => 1), 200);
Is the above a safe way to check whenever my model got saved or not?
Check if query returned a result
Eg:
$UserProduct = Product::where('seller_id', '=', $userId)->first();
if (! $UserProduct)
{
App::abort(401); //Error
}
Does above return an error if no product where found?
Check if query got executed
Eg:
$newUser = User::create([
'username' => Input::get('username'),
'email' => Input::get('email')
]);
//Check if user was created
if ( ! $newUser)
{
App::abort(500, 'Some Error');
}
//User was created show OK message
return Response::json(array('success' => true, 'user_created' => 1), 200);
Does above check if a user was created?
Check if model got saved
save() will return a boolean, saved or not saved. So you can either do:
$saved = $myModel->save();
if(!$saved){
App::abort(500, 'Error');
}
Or directly save in the if:
if(!$myModel->save()){
App::abort(500, 'Error');
}
Note that it doesn't make sense to call save() two times in a row like in your example. And by the way, many errors or problems that would keep the model from being saved will throw an exception anyways...
Check if query returned a result
first() will return null when no record is found so your check works find. However as alternative you could also use firstOrFail() which will automatically throw a ModelNotFoundException when nothing is found:
$UserProduct = Product::where('seller_id', '=', $userId)->firstOrFail();
(The same is true for find() and findOrFail())
Check if query got executed
Unfortunately with create it's not that easy. Here's the source:
public static function create(array $attributes)
{
$model = new static($attributes);
$model->save();
return $model;
}
As you can see it will create a new instance of the model with the $attributes and then call save(). Now if save() where to return true you wouldn't know because you'd get a model instance anyways. What you could do for example is check for the models id (since that's only available after the record is saved and the newly created id is returned)
if(!$newUser->id){
App::abort(500, 'Some Error');
}
You can also check the public attribute $exists on your model.
if ($myModel->exists) {
// Model exists in the database
}
I would do such move to when I use Model::create method :
$activity = Activity::create($data);
if ($activity->exists) {
// success
} else {
// failure
}
As for the Save method it's easier because $model->save() returns Bool :
$category = new Category();
$category->category_name = $request->category_name;
$is_saved = $category->save();
if ($is_saved) {
// success
} else {
// failure
}
/**
* Store a newly created country in storage.
*
* #url /country
* #method POST
* #param Request $request
* #return \Illuminate\Http\JsonResponse
*/
public function store(Request $request)
{
# Filer & only get specific parameters.
$request = $request->only('code', 'name', 'status');
# Assign login user(Auth Control).
$request['created_by'] = Auth::user()->id;
# Insert data into `countries` table.
$country = Country::create($request);
if(!$country)
throw new Exception('Error in saving data.');
}