I have written automation test cases for my application.Below is the sample code i have used for web testing.
class UserWebTestcase extends CakeWebTestCase{
var $name='UserWebTestcase';
function testLogin001()
{
//Test if new user registration form works as intended when all the inputs are given properly.
$this->get(Configure::read('url'));
$this->setField('email', 'admin45#gmail.com');
$this->setField('tmppassword', 'admin123');
$this->setField('password_confirm', 'admin123');
$this->clickSubmit('SUBMIT');
$this->assertText('login');
}
}
In test case it always gives false even though the inputs for fields are correct.The error i got like this
(Failed
C:\xampplite\htdocs\spotchase\app\tests\cases\models\user.test.php -> UserWebTestcase -> testLogin001).
Im really confused while using the assertText() method.How should i use this assertText() method and what parameters should i pass to this method. Please help.
this is not a cakephp method, but a simpletest one.
below is the actual method
/**
* Will trigger a pass if the text is found in the plain
* text form of the page.
* #param string $text Text to look for.
* #param string $message Message to display.
* #return boolean True if pass.
* #access public
*/
function assertText($text, $message = '%s') {
return $this->assert(
new TextExpectation($text),
$this->_browser->getContentAsText(),
$message);
}
So it seems that it is just looking for the actual text within the page, not a button or other element. maybe 'welcome bob' would be a better search
Related
I have a classifieds website similar to Craigslist built with laravel.
There's a status column in my MySQL posts table and I want to be able to automatically change the status of all posts that is more than 30 days old to "deleted "
So how do I do that in laravel.
There's no code here because I don't know how to go about it.
You have to use the scheduler (docs: https://laravel.com/docs/8.x/scheduling).
It allows you to perform cron tasks.
For instance, you could to something like that:
You should read the documentation first because it contains a lot of useful information to understand the following code example.
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Facades\DB;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
//
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
YourModel::query()
->where('created_at', '<', today()->subDays(30))
->update(['your_status_column' => 'deleted');
})->daily();
}
}
Here we are updating the status of all YourModel that are created since more than 30 days to deleted (and we are doing it every days at midnight).
Of course you have to adapt the code to your model and column.
You can create a command or job, and use task scheduling to automatically run the command and job you just created.
https://laravel.com/docs/8.x/scheduling
I have recorded the coded UI test using the VS2015 Coded UI Test builder. based on my recording the following function is created for my test method,
public void RecordedMethod1()
{
#region Variable Declarations
WpfText uIItemText = this.UIMainWindowWindow.UIAddNewRowControlCustom.UIGridCellCustom.UIItemText;
WpfEdit uIItemEdit = this.UIMainWindowWindow.UIAddNewRowControlCustom.UIGridCellCustom.UIItemEdit;
WpfText uIItemText1 = this.UIMainWindowWindow.UIAddNewRowControlCustom.UIGridCellCustom1.UIItemText;
#endregion
// Double-Click label
Mouse.DoubleClick(uIItemText, new Point(73, 3));
//// Failed in the following line and the test is not running after that.
// Type 'aaa' in text box
uIItemEdit.Text = this.RecordedMethod1Params.UIItemEditText;
// Double-Click label
Mouse.DoubleClick(uIItemText1, new Point(79, 10));
// Type 'bbb' in text box
uIItemEdit.Text = this.RecordedMethod1Params.UIItemEditText1;
// Type '{Enter}' in text box
Keyboard.SendKeys(uIItemEdit, this.RecordedMethod1Params.UIItemEditSendKeys, ModifierKeys.None);
}
After reaching the line to set the recorded value to the uiEditItem.Text the test case is not running further cased the failure in the test case.
I have googled for the solution and have found a one that says, you need to rewrite the test cases with the Kebord.SendKeys instead of directly setting the value to the Text property of the EditItem.
By this way I have made my code changes at the line as follows and its working.
// Type 'aaa' in text box
//uIItemEdit.Text = this.RecordedMethod1Params.UIItemEditText;
// Replaced the above line with the SenKeys
Keyboard.SendKeys(this.RecordedMethod1Params.UIItemEditText);
Is that the only solution for this problem (Manullay rewrite the test methods by using the SendKeys method instead of directly assigning a value to the uiEditItem.Text property) ? If not, please provide the feasible solution for this.
I have a view that returns search results via the search API. It performs this use case adequately and I am happy. To crown the deliverable, I need to add a title override of the form Showing search results for '%1' which looks easy enough initially but it isn't working entirely as planned.
For a URL = mysite.com/search/all?search=wombat, where the search value is gathered from an exposed form within a block, I am either getting:
Showing search results for 'Search for "all"'
or, if I enter %1 in the title override for subject not appearing in the URL, I get:
Showing search results for %1". My goal is to get "Showing search results for 'wombat'
The title override works in that it removes the Search for ... part but the substitution picks up on "all" as the exception value (or anything else that I set as the exception value) where I need to be able to pick up the value of the query string (search=wombat).
Can anyone shed some light here?
The problem is that the '%1' and '%2' that you can use to override the title refer to your path's first and second arguments (in Drupal terms) and that would be 'search' and 'all?search=wombat' in your case...
What you need instead is the 'wombat' as a path component in itself.
Perhaps you can achieve that by working that case you're talking about: the case of a "title override for subject not appearing in the URL". There is an option in the contextual filters section (I'm assuming that's where you're working) for providing a default value when one isn't present. Perhaps you can use the 'PHP code' option there, isolate your 'wombat' string and return that as a default contextual filter, and then you can get to it via the '%1'.
The php code to get that portion of the URL should look something like this:
return htmlentities($_GET['search']);
the $_GET() returns the value of that variable in the url, and the htmlentities() is just to keep it safe, since it's using a portion of the url, which is vulnerable to XSS.
See if that combo (1) setting a default argument when one isn't present and 2) using that newly set argument in your title printout) works!
I fixed this issue.
Using following two hooks we can change the defalut value of filter Programmatically.
<?php
/**
* hook_views_pre_view
* #param type $view
* #param type $display_id
* #param type $args
*/
function MODULE_NAME_views_pre_view(&$view, &$display_id, &$args) {
if ($view->name == 'VIEW_NAME') {
$filters = $view->display_handler->get_option('filters');
$view->display_handler->override_option('filters', $filters);
}
}
/**
* hook__views_pre_build
* #param type $view
* #return type
*/
function MODULE_NAME_views_pre_build($view) {
if ($view->name=='VIEW_NAME') {
$view->display['page']->handler->handlers['filter']['filter_field']->value['value'] = 8;
return $view;
}
}
?>
This code worked for me. I am using the drupal 7.
I'm building a file repository where each file has a database entry associated with it. In that database, I include the sha1 hash value for the file. Also, for security reasons, I rename each file with a specific pattern, so I can't check the filename directly via the form's filename value.
I'm trying to prevent people from uploading the same file twice, so I want to create a form validator that takes the file being uploaded and checks the hash of the file against all the values in the database. If the hash is already in the database--and thus the file already exists--the validator should return false. How access the file from inside the validator class so I can calculate the hash?
Your question is very badly asked. So I will not dig into the logic of it.Sorry Here is a very simple example of how you can create a Custom Validation Class.
The most important thing you need to remember is two methods.
isValid(): Returns either true or false through a logic
getMessages(): Returns errors messages in case of invalid.
Here is a basic example of comparing whether a user is admin or not.
class CustomValidate_UserAdmin extends Zend_Validate_Abstract
{
$admin = "username"; //I am using a static value this time
protected $_messageTemplates = array(
self::FLOAT => "'%value%' is not an admin"
);
public function isValid($value)
{
$this->_setValue($value);
if($this -> admin == $value) {
return true;
}
}
}
Now use it
$element->addValidator(new CustomValidate_UserAdmin('username'));
This is a very simple example to understand. You can replace it with your logic with a lot of ease I think.
Updates
Add validation like this...
$element->addValidator(new CustomValidate_Hash('file/location', $hashvaluettocompare));
Now in your validation class
function isValid($location, $value) {
if(hash_file("md5", $location)==$value) return true;
}
The validator you're looking for already exists it is part of Zend_File_Transfer which is used by Zend_Form_Element_File. It accepts an array to validate against.
another option would be to do the check in the controller, because you have to hash the file doing it as form validator presents difficulties.
So hash the file in the controller prior to calling ->receive() and use the Zend_Validate_Db_NoRecordExists. Might be just as easy to do this with a method in your model. You could also use this validator when creating a method that hashes the file.
The Zend_File_Transfer_Adapter_Abstract api has a method getHash(), I believe you can call getHash() and validate against Db_NoRecordExists.
Good Luck!
Code completion works only if I use:
/* #var $html HtmlHelper */
$html->link...
but I want it to work while using
$this->Html->...
any idea?
This won't be possible with the current setup. If you want code completion like that, you could put the following at the top of your class (in the constructor maybe).
if (false) {
$this->Html = new HtmlHelper();
}
This will give you autocompletion, and since the IF condition never evaluates to TRUE it won't mess up your code.