How to assert equal to all array element result? - cypress

i have this code
cy.get('body').then(($body) => {
if ($body.find('.asis-mailto-obfuscated-email').length > 0){
cy.get('.asis-mailto-obfuscated-email').invoke('text').should('eq',$email)
}else if ($body.find(':nth-child(1) > .panel-body').length > 0){
cy.log('It goes here')
}
})
})
But '.asis-mailto-obfuscated-email' returning me this
and i want all this 6 element can assert equal to $email. How can i achieved that?

Maybe use .each()
cy.get('.asis-mailto-obfuscated-email')
.each($el => {
cy.wrap($el).invoke('text').should('eq', $email)
})

If you are just checking that in the array of emails, there should be one email present, you can do this. This is assuming that $email has the email text.
cy.get('.asis-mailto-obfuscated-email').should('include.text', $email)
If you want to check that the number of emails is 6 and assuming $email has the number 6, you can do this.
cy.get('.asis-mailto-obfuscated-email').its('length').should('eq', $email)

The each will work on $body.find('.asis-mailto-obfuscated-email') as well.
$body.find('.asis-mailto-obfuscated-email')
.each(function(index) {
expect(Cypress.$(this).text()).to.eq($email)
})

Related

Email failures in Laravel

I am trying to send mail using below code
Mail::send(new ContactUs($request));
if(Mail::failures()){
return response()->json(['result' => 1]);
}
else {
return response()->json(['result' => 0]);
}
But I am not getting any response from Mail::failures() section.
Instead of using Mail::failures() you can use !empty(Mail::failures()) or count(Mail::failures()) > 0.
Mail::failures() Function returns an array of Email addresses which are failed.
array failures()
Get the array of failed recipients.
Return Value
array
You can read it here: https://laravel.com/api/5.1/Illuminate/Mail/Mailer.html#method_failures

How to create a search that uses barcode

I'm creating a search system for a point of sale that uses barcodes to search for products. When a user scans a barcode, the product with the corresponding barcode gets added to the cart. I pass the barcode to the controller using Ajax. The problem is, the resulting query is running twice doubling the order quantity when the product gets added to the cart. I don't know why this is happening.
View/Search Bar
<div class="frmSearch">
<input type="text" id="search" name="search" class="form-control" placeholder="Type Product Name..."
onmouseover="this.focus();"/>
</div>
<script type="text/javascript">
$('#search').on('keyup', function () {
$value = $(this).val();
$.ajax({
type: 'get',
url: '{{URL::to('search')}}',
data: {'search': $value},
success: function (data) {
window.location.href = "/addsale/" + data;
}
});
});
</script>
Controller
public function search(Request $request)
{
if ($request->ajax()) {
$raws = DB::table('items_masters')->where('bcode', '=', $request->search)->first();
if ($raws) {
$output = $product->id;
}
return Response($output);
}
}
Method to add a product to the cart
public function add($id)
{
$userid = Auth::user()->id;
$shop = Auth::user()->shop_id;
$product_id = $id;
$tdo = item_quantity::getQuery('item_quantities')->where([
['shop_id', '=', $shop],
['item_id', '=', $product_id]
])->get();
foreach ($tdo as $key => $tad) {
$product_quantity = $tad->quantity;
}
if (empty($product_quantity)) {
session()->flash('message', 'Item Not In Your Shop');
return redirect('/sales_area');
} elseif ($product_quantity <= 0) {
session()->flash('message', 'Item Out Of Stock');
return redirect('/sales_area');
} else {
$todo = tblcart::getQuery('tblcarts')->where([
['product_id', '=', $id], ['shop_id', '=', $shop],
['member_id', '=', $uid]
])->get();
foreach ($todo as $key => $tada) {
$cart_id = $tada->id;
}
if (empty($cart_id)) {
$tem = new tblcart;
$tem->product_id = $product_id;
$tem->quantity = 1;
$tem->shop_id = $shop;
$tem->member_id = $userid;
$tem->save();
return redirect('/sales_area');
} else {
$tem = tblcart::find($cid);
$tem->quantity = $tem->quantity + 1;
$tem->save();
return redirect('/sales_area');
}
}
}
Currently, when a user adds a product to the cart, the order quantity is doubled, so instead of one, he gets two. If the user adds the same product again, he gets four instead of two. How can I sort this out?
Looks like perhaps a timing problem, depending on if the add method is called directly from the search. With keyup, you run a possible risk of triggering the search function at least once, and possibly more than once based on the user still typing the product they want even after the original ajax request has been sent based on the previous character typed within the search bar.
Suggestion would be to add a time delay on either the keyup function, or within your search code. Say something like (pseudo code) within the controller
if($timeOfLastTransaction < 30 seconds && $productId == $previousProductId)
// cancel this duplicate
else
set $timeOfLastTransaction
set $previousProductId
I'm still not sure what's calling the add() method, and this would be a good start for bug hunting the specific doubling-up error.
But, looking at that code in the add() method, there are a number of things that you might clean slightly -- and may resolve your issue. Suggest naming the $id variable $product_id directly in the method's params instead of renaming it in the top part -- you use two different vars for the same thing in multiple places, and it is a little confusing.
A little bigger issue is that you are potentially re-assigning the same variable in several places after pulling a collection from the database:
$tdo = item_quantity::getQuery('item_quantities')->where( [['shop_id', '=', $shop],
['item_id', '=', $product_id]])->get();
foreach ($tdo as $key => $tad) {
$product_quantity=$tad->quantity;
}
This line and the next DB pull below it both loop through a collection and re-assign / overwrite $product_quantity in this case and $cart_id in the next. Why not use ->first() and skip the loop? I think this might reduce any unforeseen errors as well

Validate Two Inputs which depends on each other

I have two Inputs 'creditor' and 'debtor' , I want The user to put value in one of them at least, or both.
this is the approach i am using
if($request->input('creditor')==Null && $request->input('debtor')==Null){
Session::flash('danger','Please add Value in one of the two inputs at least');
return redirect()->back();
}
How to do the same thing with laravel validation
like in example :
$request->validate([
'creditor' => 'required',
'debtor' => 'required',
]
but what i want if one of the inputs has avalue there is no need to check for the other
There are many ways to tackle this problem but I recommend you use $request->filled() as it checks if a field is present and that it has value. So, it does isset() and !empty() together.
The || AKA OR logic returns true if one of the statements/conditions is true and returns false if both conditions are false.
if($request->filled('creditor') || $request->filled('debtor')){
// Either of the has value in it.
} else {
Session::flash('danger','Please add Value in one of the two inputs at least');
return redirect()->back();
}
EDIT
Your could also use required_without_all:foo,bar,
An Example:
$rules = array(
'creditor' => 'required_without_all:debtor',
'debtor' => 'required_without_all:creditor',
);
$validator = \Validator::make($request->all(), $rules);
if($validator->fails()){
Session::flash('danger','Please add Value in one of the two inputs at least');
return redirect()->back();
}
You can simply check with OR operator..

Laravel 5.4 Collection Map Return Values

I'm having trouble creating a function on collection map with return values.
public function getCollectionFakeId($collection, $fieldNames){
$optimus = $this->optimus;
$result = $collection->map(function($item, $key) use ($optimus, $fieldNames) {
return [
$fieldNames[0] =>$optimus->encode($item->id),
$fieldNames[1] => $item->lastname
];
}) ;
dd($result);
return json_decode(json_encode($result), FALSE);
}
As you can see the return fieldNames[0] is being hardcoded. I don't know how many fieldNames it will received. I need to return those fieldnames with obfuscated Id. So basically The only changed is the Id. Here is the screenshot.
As you can see the fieldNames are just 2 but what if it becomes 5 or 6. I don't really know how many fieldNames they are going to pass in the parameter. How can I return it. Thanks.
In case someone will encounter this problem. Here is my solution...
public function getCollectionFakeId($collection, $fieldNames){
$optimus = $this->optimus;
$result = $collection->map(function($item, $key) use ($optimus, $fieldNames) {
$mapFieldNames = array_map(function($v) use ($optimus, $item) {
if( $v == 'id'){
return $optimus->encode($item->id);
}
else{
return $v;
}
}, $fieldNames);
return $mapFieldNames;
}) ;
dd($result);
return json_decode(json_encode($result), FALSE);
}
The result is the same. AWESOME!

codeigniter join table ss

I have a problem with codeigniter,
I want to do a join in the addition of a carrier,
when I add I assign a truck this driver
I want the state of truck changes from 0 to 1,
but I do not know,
public function add($email, $password , $nom , $prenom , $telephone,$id_camion)
{
$query = $this->db->get_where('transporteur', array('email' => $email));
if ($query->num_rows == 1) {
return FALSE;
}
$this->db->insert('transporteur', array('email' => $email,'password' => md5($password),'nom' => $nom ,'prenom'=>$prenom ,'telephone' => $telephone,'id_camion' => $id_camion));
return TRUE;
}
If I understand your question correctly, now that you've inserted a new carrier you want to set some state in a table of trucks. You already have the truck ID as a parameter so in theory all you need to do is:
//update only on a given camion_id
$this->db->where('id', $camion_id);
$this->db->update('camions', array('state' => 1));
Here I assume your table is called camions, its ID is id and the state column you're trying to change from 0 to 1 is called state.
If that's not quite right, please update your question. If you have trouble translating it into english, I can help with that, too. ;)
I'm confused about your question but you have (num_rows should be num_rows()) following code:
if ($query->num_rows == 1) {
return FALSE;
}
It should be:
if ($query->num_rows() == 1) {
return FALSE;
}
It's a method not a property. You may also use it like this way:
if ($query->num_rows()) {
return FALSE;
}

Resources