Comments (7)
@FzSalehi , @sinamiandashti
I had the same problem today. Here my solution for it. Not pretty, but does the work. 😜
Some explanation:
You can not get the captcha value, because it is hashed after the creation and only the hash is obtained.
See: https://github.com/mewebstudio/captcha/blob/master/src/Captcha.php#L343
The comparison is done via hashing and comparing the provided value with existing hash.
See: https://github.com/mewebstudio/captcha/blob/master/src/Captcha.php#L478
The good thing is, that the author used dependency injection and all the properties and methods are public or protected, so you can easily override necessary parts and replace the base class. 👍
Here my simple Fake Class solution:
<?php
namespace Tests;
use Mews\Captcha\Captcha;
class CaptchaFake extends Captcha
{
public static string $expectedValue = '';
public static function fake(string $value = ''): void
{
self::$expectedValue = $value;
app()->bind('captcha', function ($app) {
return new CaptchaFake(
$app['Illuminate\Filesystem\Filesystem'],
$app['Illuminate\Contracts\Config\Repository'],
$app['Intervention\Image\ImageManager'],
$app['Illuminate\Session\Store'],
$app['Illuminate\Hashing\BcryptHasher'],
$app['Illuminate\Support\Str']
);
});
}
public function check(string $value): bool
{
return self::$expectedValue === $value;
}
}
I have added a static expected value property, so I can easily simulate success and failure.
Just call the fake method at the beginning of your test method:
CaptchaFake::fake('123456');
I did not wanted to mock a lot so I just copied the creation of the original Captcha Class from the provider.
See: https://github.com/mewebstudio/captcha/blob/master/src/CaptchaServiceProvider.php#L73
If you use the check_api method, you can fake it as well.
https://github.com/mewebstudio/captcha/blob/master/src/Captcha.php#L505
Hope it helps you as well. Enjoy 😉
from captcha.
Oohh man, @Sergej-Tihonov. Thank so much. It's work!
from captcha.
Hi
You can use it like below
If used API
Captcha::shouldReceive('check_api')->andReturn(true);
If did not use API
Captcha::shouldReceive('check')->andReturn(true);
from captcha.
same question
from captcha.
@sinamiandashti
thank you for your help .. I have also a solution which is not so pretty too 😜 but it is simpler
you can modify laravel request validations .. right ?
so just put a simple condition like so:
// in some controller
public function store(Request $request)
{
//here we decide which inputs required
$requiredInputs = $this->getInputRequired();
//then do the validation ...
$validatedData = $request->validate($requiredInputs );
// The blog post is valid...
}
private function getInputRequired(){
// this is golden condition
if(env('debug')){
return [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
];
return [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
'captcha' => 'required',
];
}
}
in this way .. you will never need to pass captcha input from your test requests.
But anyways I suggest to @mewebstudio to do something about the testing environment.
I think @Sergej-Tihonov 's solution could be implemented in this package.
from captcha.
expectedValue
it's a very bad practice, never mix test and production code.
from captcha.
Hi You can use it like below
If used API
Captcha::shouldReceive('check_api')->andReturn(true);
If did not use API
Captcha::shouldReceive('check')->andReturn(true);
thank you, this work for me, I use FormRequest validation and it's working
from captcha.
Related Issues (20)
- margin top issue
- Captcha img not refreshing onclick()
- Accessibility issue on a web page
- libpng warning: iCCP: known incorrect sRGB profile
- captcha-jwt
- Support for 8.2 HOT 1
- Can math add subtraction, multiplication, and division
- Support for laravel 10
- Captcha Value always gives output wrong.
- The captcha is not display image on cpanel laravel 10
- Captcha Value always gives invalid value HOT 1
- Are there plans to upgrade intervention/image to version 3.x?
- Request for Laravel 11 Support HOT 3
- An error will be prompted under window
- How to use on inertiajs laravel HOT 2
- 关于mews/captcha:3.3版本验证用户输入验证码是否正确判断存在问题 HOT 1
- How to set up config file for simple math calculations HOT 1
- alt="" for Captcha::img()
- intervention/image 3 HOT 5
- Installation errors.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from captcha.