Laravel complex validation feature testing - php

If i have a Validator like below
[
'id' => ['string', 'required', 'regex:/^[A-Za-z]{1}([A-Za-z]|[0-9]){5,7}$/im', 'unique:users'],
'user_name' => ['string', 'required', 'unique:users'],
'email' => ['string', 'required', 'email', 'unique:users'],
'password' => [\Illuminate\Validation\Rules\Password::min(8)->letters()->mixedCase()->numbers()->symbols(), 'max:32', 'sometimes'],
'role_id' => ['integer', 'required'],
'status' => ['integer', 'required', 'in:1,2']
]
That would requires 4 * 3 * 4 * 3 * 2 * 3 = 846 methods to test all the validation cases, which sounds stupid itself.
How would validation feature testing (and other kinds of testing) actually be implemented?

Related

how can i send an email when user register but to the specific email in Laravel 8

I need when a user registers successfully to send notification email to the reference email field
the notification include one line text with link buttom and when he click on the buttom will redirect to my website
I need to send that notification to the referance_email field
public function create(array $input)
{ $massage = ['tc_no_pasaport_no.unique'=> 'Sorry, internal error .',
'phone.unique'=>'Sorry, internal error .' ];
Validator::make($input, [
'phone' => ['required', 'string', 'max:255','unique:users'],
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'tc_no_pasaport_no' => ['required','max:11','unique:users'],
'place_of_birth' => ['required'],
'date_of_birth' => ['required'],
'educational_status' => ['required','string'],
'school_department' => ['required'],
'address' => ['required'],
'password' => $this->passwordRules(),
'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['required', 'accepted'] : '',
],$massage)->validate();
$users = User::where('email', '=', $input['email'])->first();
if ($users === null) {
return User::create([
'name' => $input['name'],
'email' => $input['email'],
'phone' => $input['phone'],
'password' => Hash::make($input['password']),
'membership_status' =>'pasif',
'membership_type' =>'standard',
'last_activation_date' => Carbon::now(),
'membership_end_date' => Carbon::now(),
'temporary_id' => random_int(1000000, 9999999),
'tc_no_pasaport_no' => $input['tc_no_pasaport_no'],
'place_of_birth' => $input['place_of_birth'],
'date_of_birth' => $input['date_of_birth'],
'educational_status' => $input['educational_status'],
'school_department' => $input['school_department'],
'Institution_and_unit' => $input['Institution_and_unit'],
'address' => $input['address'],
'referance_email' => $input['referance_email'],
'letter_of_Intent'=>$input['letter_of_Intent'],
'created_at' => Carbon::now(),
]);
}
You can use PHP Mailer library to send email in PHP.

Laravel array distinct validation

I have the following request validation rules in an controller.
public function __invoke(Request $request)
{
$this->validate($request, [
'name' => ['required', 'unique:games,name', 'string'],
'variant' => ['required', 'string'],
'rules' => ['string'],
'is_anonymous' => ['boolean', 'required'],
'message_mode' => ['required', new EnumRule(MessageModeEnum::class)],
'powers' => ['required', 'array'],
'powers.*' => ['distinct:ignore_case', 'required', 'string'],
]);
}
Below is the test I'm running against the controller. I would expect the powers.* validation to throw an error as the two powers are equal and not distinct.
/**
* #test
*
*/
public function endpoint_validation()
{
$user = User::factory()->create(['rank' => RankEnum::A()]);
$response = $this->actingAs($user)->post(route('game.create'), [
'name' => '::name::',
'variant' => '::variant::',
'rules' => '::rules::',
'is_anonymous' => false,
'message_mode' => 'none',
'powers' => [
'::power::', '::power::'
],);
$response->assertSessionHasErrors('powers');
}
However, this test is failing. What am I doing wrong? How can I fix this?

Laravel validation rules are not applying properly

I'm trying to apply these validation rules to my controller function, but any of the rules are not applying
Here is my code
if($request->hasFile('propic'))
{
$this->validate($request, [
'name' => 'required', 'alpha','min:2', 'max:255',
'last_name' => 'required', 'alpha','min:5', 'max:255',
'mobile' => 'required', 'string','min:10','max:14', 'regex:/\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|
2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|
4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}$/',
'email' => 'required', 'string', 'email', 'max:255', 'unique:users,email,'.$setting->id.'',
'propic' => 'required','image','mimes:jpeg,png,jpg,gif,svg','max:2048',
]);
$imageName = time().'.'.$request->propic->extension();
$request->propic->move(public_path('propics'), $imageName);
$setting->propic=$imageName;
$setting->name=$request->input('name');
$setting->last_name=$request->input('last_name');
$setting->mobile=$request->input('mobile');
$setting->email=$request->input('email');
$setting->update();
return Redirect::back()->with('success',__('sentence.User updated successfully'));
}
At time of writing, there's two accepted formats for passing in validation rules:
As an array of strings (note the square brackets, which is what you are missing currently):
$this->validate($request, [
'name' => ['required', 'alpha','min:2', 'max:255'],
...
]);
As a single pipe-delimited string:
$this->validate($request, [
'name' => 'required|alpha|min:2|max:255',
...
]);

Laravel Validating An Array in Update Method With Multiple Rows Needing Ignoring

I'm trying to validate an array within the update method which means I need to ignore the id's of the rows so that it doesn'r return the error:
contactName has already been taken
Here is my validation from my update method of my controller:
public function update($id, Request $request)
{
$brand = Brand::findOrFail($id);
$request->validate([
'name' => ['required', 'string', 'max:255', Rule::unique('brands')->ignore($id)],
'contactNames' => ['required', 'array'],
'contactNames.*' => ['required', 'max:255', 'string', 'distinct', Rule::unique('brand_managers', 'name')->ignore( //what goes here? )],
'emails' => ['required', 'array'],
'emails.*' => ['required', 'max:255', 'email', 'distinct', Rule::unique('brand_managers', 'email')->ignore( //what goes here? )],
'contactNumbers' => ['array'],
'contactNumbers.*' => ['numeric'],
'groupCheckbox' => ['required', 'min:1'],
]);
}
With the unique validation rule on 'name' I can ignore the id that is coming with the $id using route model binding, but with the contactNames and contactEmails this needs to check a table that has a manyToMany relationship -
how do I ignore the id's of the brand_manager rows for multiple validation checks?
I will try and edit my question for more clarity
Thanks!
You can easily create validation rules dynamically as you wish.
Here is example from my current project which requires unique field except self.
use Illuminate\Validation\Rule;
$unique_check = Rule::unique('brand_managers', 'email'); // table with some unique rows
//if you need ignore multiple rows/multiple conditions
$unique_check->where(function ($query) use ($brand) {
return $query->whereNotIn('id', $brand->brand_managers->pluck('id')->toArray());
});
$request->validate([
'emails.*' => [ // promocode unique string column
'required',
$unique_check
],
]);
$request->validate([
'name' => ['required', 'string', 'max:255', Rule::unique('brands')->ignore($id)],
'contactNames' => ['required', 'array'],
'contactNames.*' => ['required', 'max:255', 'string', 'distinct',.$brand->id, // ID of record which want to ignore
'emails' => ['required', 'array'],
'emails.*' => ['required', 'max:255', 'email', 'distinct', .$brand->id, // ID of record which want to ignore
'contactNumbers' => ['array'],
'contactNumbers.*' => ['numeric'],
'groupCheckbox' => ['required', 'min:1'],
]);`
pass id want to ignore at time of update records.
more information check https://laravel.com/docs/5.7/validation

laravel 5.2 Method [validate1] does not exist

My validation fails with this:
$this->validate($request, [
'name' => ['required'],
'email' => ['required', 'email', 'unique:organisers',$organiser->id,'organisers_id'],
'organiser_logo' => ['mimes:jpeg,jpg,png', 'max:10000'],
]);
but it works with this:
$this->validate($request, [
'name' => ['required'],
'email' => ['required', 'email', 'unique:organisers'],
'organiser_logo' => ['mimes:jpeg,jpg,png', 'max:10000'],
]);
This:
'unique:organisers',$organiser->id,'organisers_id'
needs to be:
'unique:organisers,'.$organiser->id.',organisers_id'
or (note the double-quotes):
"unique:organisers,{$organiser->id},organisers_id"
The , means "new array element", the . means "add to this string".

Categories