How to click link icon with laravel dusk? - php

If I have a link:
Click Me
I know I can clickLink based on its text.
public function testCanClickLink()
{
$this->browse(function ($browser) {
$browser->visit('/welcome')
->clickLink('Click Me');
});
}
But how can I click an icon link?
<a href="/somewhere">
<i class="fa fa-plus" aria-hidden="true"></i>
</a>

You can target the href like this:
->click('a[href="/somewhere"]')

This is a bit hacky, but it's what I've come up with as a workaround.
Put an id selector on the link.
<a id="link-click-me" href="/somewhere">
<i class="fa fa-plus" aria-hidden="true"></i>
</a>
Assert it's visible.
Get the href attribute.
Visit it.
Assert path is correct.
public function testCanClickLink()
{
$this->browse(function ($browser) {
$browser->visit('/welcome')
->assertVisible('#link-click-me')
->visit(
$browser->attribute('#link-click-me', 'href')
)
->assertPathIs('/somewhere');
});
}

Related

Open modal when redirected only

Before, I had a view called "book-edit/[ID]" to edit a book but now I added the form to edit the book in the book details page. I have a button to edit in the list of books page and I want it to go to the book page and open de edit modal on click. This is what I have rn:
View: Books (datatable list with this buttons for each):
<a href="<?php setURL(); ?>./book/<?php echo $autor->fields["id"]; ?>" class="btn btn-sm btn-clean btn-icon btn-icon-md" title="View Book">
<i class="fas fa-eye"></i>
</a>
<a href="<?php setURL(); ?>./copy-add/<?php echo $autor->fields["id"]; ?>" class="btn btn-sm btn-clean btn-icon btn-icon-md" title="Add Copy">
<i class="fas fa-plus"></i>
</a>
<a href="<?php setURL(); ?>./book-edit/<?php echo $autor->fields["id"]; ?>" class="btn btn-sm btn-clean btn-icon btn-icon-md" title="Edit Book">
<i class="fas fa-edit"></i>
</a>
View: Book/[ID] (I have modals with id "#editBookModal" and "#addCopyModal")
When you click on the books list to edit the book or add the copy, I want it to go to book/[ID] and automatically open the intended modal. But only when buttons clicked, if you go to Book/[ID] to view the book, it shouldn't open the modals automatically. That's why I can't just open it with jquery in the book/[ID] page...
Any idea of how I can do this?
Thanks!
One approach would be add hashes to the url in the links like:
....
Then on the other page check the url hash and if it matches a modal then open that modal
const modalHashes = {
'#edit-book':'#editBookModal',
'#copy-book' : '#addCopyModal'
}
let hash = window.location.hash;
// inside document.ready and after modal script has loaded
if(hash && modalHashes[hash]){
const modalSelector = modalHashes[hash];
$(modalSelector).yourModalShowMethod()
}
You could even remove the hash when the modal gets closed so if user refreshes the page it won't pop up again
// in modal close event handler
window.location.hash =''

The value does not change in the database

I made a button to update a user's access to the website and I get an error. I searched here but I did not find the answer to help me, can you give me some advice?
This is the error: Creating default object from empty value
Controller:
public function suspendUser(Request $request, $id) {
$user = User::find($id);
$user->userAcces = 0;
$user->save();
return back();
}
View::
<div class="col-md-3 col-sm-3">
<form>
<i class="fa fa-eye" aria-hidden="true"></i>
<i class="fa fa-ban" aria-hidden="true"></i>
<i class="fas fa-trash"></i>
<i class="fas fa-user-friends"></i>
<form>
</div>
Route:
Route::get('/updateUser/{id}', 'UserController#suspendUser');
I tried other things but failed, maybe something went wrong in the controller?
It means this user is not found with this $id and $user is null , so check the $id which sent to the suspendUser method, and it's better to use User::findOrFail($id); in order to return 404 if model not found ,and not face these kinds of errors.

laravel a href validation

I would like to ask if it is possible to validate a href element in laravel.
For example, I'm having a bunch of people responses in my guest book and each user is able do delete his posts. I'm deleting it like this:
<div class="col-lg-2">
<ul class="list-unstyled trash">
<li>
<a href="/responses/{{$response->id}}">
<span onclick="alertDialog()" class="glyphicon glyphicon-trash"></span></a></li>
<li><button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#{{$response->id}}">Edit</button></li>
</ul>
</div>
Unfortunately, every single user can change "/responses/{{$response->id}}"> id via browser developer tools and delete other user's responses. Is there any possible solutions to prevent this issue?
Just check the logged user before rendering that html section:
<div class="col-lg-2">
<!-- assuming post is your variable and user is a property which references the user who created the record -->
#if($post->user == Auth::id())
<ul class="list-unstyled trash">
<li>
<a href="/responses/{{$response->id}}">
<span onclick="alertDialog()" class="glyphicon glyphicon-trash"></span></a></li>
<li><button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#{{$response->id}}">Edit</button></li>
</ul>
#endif
</div>
This way only the user who owns the post will be able to see the button in the html.
You need to use policies to check if the user that tries to delete a record is its owner:
public function delete(User $user, Model $model)
{
return $user->id === $model->user_id;
}
Or you can do it manually in a controller method:
if (auth()->id() === $model->user_id) {
// Delete the record
}
You should make /responses/{{$response->id}} a POST route and verify that the passed id matches with the authenticated users id. ( Auth::user()->id;)
I ended up checking like this in my destroy(id) method:
$count = Comment::where(['response_id' => $id])->count();
if (($responses->where('id',$id)->value('guests_email') == Cookie::get('email') && ($count==0)) || (Auth::check()))

Laravel: Cannot route to expected URL

Here is my nav-bar:
<div class="col-md-2">
<ul class="list-group-item">
<li><i class="fa fa-fw fa-file</i> All Post
</li>
<li><i class="fa fa-fw fa-plus-circle"></i> Create New Post</li>
<li><i class="fa fa-fw fa-tasks"></i> Manage Posts</li>
</ul>
</div>
and here is my route.php
Route::group(['prefix' => 'posts'], function(){
Route::get('', 'PostController#index');
Route::get('create', 'PostController#create');
Route::post('confirm', 'PostController#confirmation');
Route::get('{postID}', 'PostController#show');
Route::get('posts/manage', 'PostController#manage');});
I expect when I click on the "Manage Posts" button, it will redirect me to function manage() in my PostController.
But when I click on it, it redirects to a view which belongs to storage/framework/views which is show() in my PostController.
I don't know why and how to make it to the right url.
Can somebody help me with this one please?
Thank you.
First of all, your link links to /posts/management, not /posts/manage. Second, you already have the prefix posts for this route-group, so the route posts/manage will be available under the url /posts/posts/manage.
You also want to move the manage route before your {postID} route, because {postID} will just catch anything, so the router has to first check the manage-route, and only if it doesn't match, the catch-all route.
And you should control what is accepted as a valid postID using Route Parameters: Regular Expression Constraints.
Route::get('{postID}', 'PostController#show')->where('id', '[0-9]+');

How can I get a URL for a controller action?

I have a link that looks like this:
<a class="btn btn-primary edit"
href="Personas/details/<?php echo $persona['Persona']['id']; ?>"
data-original-title="Editar">
<i class="icon-pencil icon-white"></i>
</a>
As you can see I'm manually writing in the controller and action for the link. Is there some way to make this not as brittle?
Something like:
<a href="Url.Action("Personas", "Details", array('id', 1);" >Asdf</a>
Use: Html->url, does exactly what you want.

Categories