PHPUNIT Zend framework how mock Zend_Registry::set to be tested against 1st datasource item only and unset it for the 2nd? - zend-framework

I'm having an issue with creating this UT with PHPUNIT and Zend framework, i'm quite new its my second UT...
This is the function to test:
this is the function which the UT is made for:
protected function _functionToTest( $view ) {
if($this->domagick){
$view->details = array(
        'important value' => Zend_Registry::get('importantvalue')
                   'Some String' => $this->_getZendHelper()
);
}
}
Note that  $this->_getZendHelper() is the function that brings in the Zend_Registry::set('importantValue', '123');
It fails the second assertion, should I create 2 separate tests?
Is there any way to make
Zend_Registry::set('importantValue', '123');
to run against the first data provider (data1)
and then on the second run, disable it? as it should be Null, and it is creating an array instead of null.
Or should I mock it a particular manner?
my test code:
   /** #dataProvider functionToTestDataProvider
*/
    public function testfunctionToTest($message, $siteMagicSettings, $expected)
    {
// this is the value that is I cant understand how to "reset"
// for the second dataprovider test which expects null
        Zend_Registry::set('importantValue', '123');
        $myMockForDebug = $this->getMockBuilder('CLASS_Name_Jack')
            ->disableOriginalConstructor()
            ->setMethods(array('_getZendHelper, _functionToTest'))
            ->getMock();
        $myMockForDebug = $this->getPublicClass($myMockForDebug);
        $myMockForDebug->_siteMagicSettings = $siteMagicSettings;
        $myMockForDebug->expects($this->any())
            ->method('_getZendHelper')
            ->will($this->returnValue('hello world'));
        $zendFrameworkControllerFrontEndMockVariable= $this->getMockBuilder('Zend_Controller_Front')
            ->disableOriginalConstructor()
            ->setMethods(
                array(
                    'getMagicalContext';
                )
            )
            ->getMock();
        $myMockForDebug->_zendControllerFront = $zendFrameworkControllerFrontEndMock;
        $view = $myMockForDebug->_zendControllerFront;
        $myMockForDebug->_functionToTest($view);
        $this->assertEquals($expected , $view->details);
        $this->assertNull($expected , $view->details);
    }
my data provider:
    public function functionToTestDataProvider()
    {
        return [
            'data1'=> [
                    'message' => 'Do magick is true so it should create expected',
                    'siteMagicSettings' => (object) array(
                        'values' => (object) array(
                            'doMagick'=> 'true'
                        )
                    ),
                    'expected' => array(
                            'important value' => '123',
                            'Some String' => 'hello world'
                    ),
                ],
            'data2'=> [
                    'message' => 'Do magick is turned off',
                    'siteMagicSettings' => (object) array(
                        'values' => (object) array(
                            'doMagick'=> 'false'
                        )
                    ),
                    'expected' => null
                ]
            ];
    }
currently when I run the test , I get 1 Failure:
"data2" ('Do magick is turned off', stdClass, NULL)
Array (...) does not match expected type "NULL".

After all the comments I'll try to make an answer for this.
The main issue from my point of view is that your method to test is too much coupled with Zend components, that do not need to be tested, they are already tested by the framework.
So this is how I would refactor your code
protected function _functionToTest($carData, $helperString) {
return [
'important value' => $carData
'Some String' => $helperString
];
}
// ..and somewhere
$helperString = $this->_getZendHelper();
$view->details = $this->domagick ? $this->_functionToTest($this->car, $helperString) : [];
Your function does actually make nothing more than creating an array.

Related

What approach should be made to successfully use a specific variable?

I have a function were in I need to update multiple dropdown lists.
Since I want to remove manual interventions, I want the test to automatically decide which data to be used.
I have 2 sets of array/variable on my main tests.
arrDefinition1 = ['Section;Sec1', 'Domain;test1', 'Process layer;test1', 'Skill;test1'];
arrDefinition2 = ['Section;Sec2', 'Domain;test2', 'Process layer;test2', 'Skill;test2'];
If the current value of the dropdown list is "Sec1" then arrDefinition2 will be used and vise-versa.
But due to promises, my IF condition was executed first before the IT and the variable that contains the value of getText contains "undefined", which means arrDefinition2 will always be used.
it('should click the Edit icon of the first row of Activity table', function() {
waitForElement(5, elmFirstRow, true);
editFirstRow.click();
waitForElement(5, element(by.id('section')), true);
element(by.id('section')).getText().then(function (tmpText) {
tempObject.strSection = tmpText;
});
});
if (tempObject.strSection == 'Sec1') {
console.log('first');
fillUpForm(0, tempObject.arrDefinition2, 'UpdateMDef');
} else {
console.log('second');
fillUpForm(0, tempObject.arrDefinition1, 'UpdateMDef');
}
Note: The function fillUpForm consists of IT functions.
Any suggestions are very much appreciated.

Get Line Items in an Invoice logic hook in SuiteCRM

Via a logic hook I'm trying to update fields of my products, after an invoice has been saved.
What I understand so far is, that I need to get the invoice related AOS_Products_Quotes and from there I could get the products, update the required fields and save the products. Does that sound about right?
The logic hook is being triggered but relationships won't load.
function decrement_stocks ( $bean, $event, $arguments) {
//$bean->product_value_c = $bean->$product_unit_price * $bean->product_qty;
$file = 'custom/modules/AOS_Invoices/decrement.txt';
// Get the Invoice ID:
$sInvoiceID = $bean->id;
$oInvoice = new AOS_Invoices();
$oInvoice->retrieve($sInvoiceID);
$oInvoice->load_relationship('aos_invoices_aos_product_quotes');
$aProductQuotes = $oInvoice->aos_invoices_aos_product_quotes->getBeans();
/*
$aLineItemslist = array();
foreach ($oInvoice->aos_invoices_aos_product_quotes->getBeans() as $lineitem) {
$aLineItemslist[$lineitem->id] = $lineitem;
}
*/
$sBean = var_export($bean, true);
$sInvoice = var_export($oInvoice, true);
$sProductQuotes = var_export($aProductQuotes, true);
$current = $sProductQuotes . "\n\n\n------\n\n\n" . $sInvoice . "\n\n\n------\n\n\n" . $sBean;
file_put_contents($file, $current);
}
The invoice is being retrieved just fine. But either load_relationship isn't doing anything ($sInvoice isn't changing with or without it) and $aProductQuotes is Null.
I'm working on SuiteCRM 7.8.3 and tried it on 7.9.1 as well without success. What am I doing wrong?
I'm not familiar with SuiteCRM specifics, however I'd always suggest to check:
Return value of retrieve(): bean or null?
If null, then no bean with the given ID was found.
In such case $oInvoice would stay empty (Your comment suggests that's not the case here though)
Return value of load_relationship(): true (success) or false (failure, check logs)
And I do wonder, why don't you use $bean?
Instead you seem to receive another copy/reference of $bean (and calling it $oInvoice)? Why?
Or did you mean to receive a different type bean that is somehow connected to $bean?
Then its surely doesn't have the same id as $bean, unless you specifically coded it that way.

SugarCRM 6.5 CE: how to remove button in detailview according to a condition

I'm trying to remove buttons in detail view of a Lead if it is alredy converted.
I saw a similar question and it use javascript to hide buttons. I'm trying to obtain same result via php.
This is my view.detail.php in custom\modules\Leads\views\ folder
class LeadsViewDetail extends ViewDetail {
function __construct(){
parent::__construct();
}
function preDisplay() {
parent::preDisplay();
if($this->bean->converted==1) {
echo "hide";
foreach ($this->dv->defs['templateMeta']['form']['buttons'] as $key => $value) {
unset($this->dv->defs['templateMeta']['form']['buttons'][$key]);
}
} else {
echo "show";
}
}
}
Using this code, after a Quick Repair & Rebuilt, I see "hide" or "show" correctly according to the Lead status but buttons are not updated correctly.
If I open a converted Lead after QR&R, I will never see the buttons.
If I open a unconverted Lead after QR&R, I will see the buttons all times.
I'm stuck with this situation. Can anyone explain me where is the problem? How I can solve it?
Every help is very appreciated.
You can probably handle this without extending the ViewDetail by using Smarty logic ("customCode") in custom/modules/Leads/metadata/detailviewdefs.php. It looks like the Convert button is already only rendered when the user has Edit privileges, so it's not a big deal to add one more condition to it...
$viewdefs['Leads']['DetailView']['templateMeta']['form]['buttons'][] = array('customCode' => '
{if $bean->aclAccess("edit") && $bean->converted}
<input title="{$MOD.LBL_CONVERTLEAD_TITLE}"
accessKey="{$MOD.LBL_CONVERTLEAD_BUTTON_KEY}"
type="button"
class="button"
name="convert"
value="{$MOD.LBL_CONVERTLEAD}"
onClick="document.location=\'index.php?module=Leads&action=ConvertLead&record={$fields.id.value}\'" />
{/if}');
Alternatively, if you do have several conditions and they'd get too messy or difficult for Smarty logic to be reasonable, we can combine a small amount of Smarty Logic with the extended ViewDetail.
This except of custom/modules/Leads/metadata/detailviewdefs.php is actually the out-of-the-box file from SugarCRM CE 6.5.24, where it looks like they've actually tried to make this customization easier by supplying a Smarty var $DISABLE_CONVERT_ACTION. For reference, it simply needs the global config variable disable_convert_lead to be set and enabled, but I suspect that this was a relatively new feature not included in earlier versions. Still, it's a good example of using the View to set a simple Smarty variable that we can pivot on:
<?php
$viewdefs['Leads']['DetailView'] = array (
'templateMeta' => array (
'form' => array (
'buttons' => array (
'EDIT',
'DUPLICATE',
'DELETE',
array (
'customCode' => '{if $bean->aclAccess("edit") && !$DISABLE_CONVERT_ACTION}<input title="{$MOD.LBL_CONVERTLEAD_TITLE}" accessKey="{$MOD.LBL_CONVERTLEAD_BUTTON_KEY}" type="button" class="button" onClick="document.location=\'index.php?module=Leads&action=ConvertLead&record={$fields.id.value}\'" name="convert" value="{$MOD.LBL_CONVERTLEAD}">{/if}',
//Bug#51778: The custom code will be replaced with sugar_html. customCode will be deplicated.
'sugar_html' => array(
'type' => 'button',
'value' => '{$MOD.LBL_CONVERTLEAD}',
'htmlOptions' => array(
'title' => '{$MOD.LBL_CONVERTLEAD_TITLE}',
'accessKey' => '{$MOD.LBL_CONVERTLEAD_BUTTON_KEY}',
'class' => 'button',
'onClick' => 'document.location=\'index.php?module=Leads&action=ConvertLead&record={$fields.id.value}\'',
'name' => 'convert',
'id' => 'convert_lead_button',
),
'template' => '{if $bean->aclAccess("edit") && !$DISABLE_CONVERT_ACTION}[CONTENT]{/if}',
),
),
We can combine this $DISABLE_CONVERT_ACTION reference with a custom/modules/Leads/views/view.detail.php like the following to set it based on whatever condition we want:
<?php
require_once('modules/Leads/views/view.detail.php');
class CustomLeadsViewDetail extends LeadsViewDetail {
/*
* while we might normally like to call parent::display() in this method to
* best emulate what the parnts will do, we instead here copy-and-paste the
* parent methods' content because LeadsViewDetail::display() will set the
* DISABLE_CONVERT_ACTION Smarty var differently than we want.
*/
public function display(){
global $sugar_config;
// Example One: Disable Conversion when status is Converted
$disableConvert = ($this->bean->status == 'Converted');
// Example Two: Disable Conversion when there is at lead one related Call
// where the status is Held
$disableConvert = FALSE;
$this->bean->load_relationships('calls');
foreach($this->bean->calls->getBeans() as $call){
if($call->status == 'Held'){
$disableConvert = TRUE;
break; // exit foreach()
}
}
// Example Three: Disable Conversion if the User is in a specific Role, e.g.
// Interns who are great for data entry in Leads but shouldn't be making
// actual sales
global $current_user;
$disableConvert = $current_user->check_role_membership('No Lead Conversions');
// In any of the above examples, once we have $disableConvert set up
// as we want, let the Smarty template know.
$this->ss->assign("DISABLE_CONVERT_ACTION", $disableConvert);
// copied from ViewDetail::display();
if(empty($this->bean->id)) {
sugar_die($GLOBALS['app_strings']['ERROR_NO_RECORD']);
}
$this->dv->process();
echo $this->dv->display();
}
}

Codeigniter form validation callback rule issue

I am using Codeigniter 3.x form validation callback method in combination trim and required to validate a field.
The problem is, when I pipe them: trim|required|callback_some_method, the callback method seems to take precedence over trim and required and shows its error message.
Any ideas on this?
EDIT:
This is the rule:
$this->form_validation->set_rules('new_password', 'New Password', 'trim|required|min_length[8]|callback_password_check');
And this is the password_check method:
function password_check($pwd) {
$containsLetterUC = preg_match('/[A-Z]/', $pwd);
$containsLetterLC = preg_match('/[a-z]/', $pwd);
$containsDigit = preg_match('/\d/', $pwd);
$containsSpecial = preg_match('/[^a-zA-Z\d]/', $pwd);
if ( !($containsLetterUC && $containsLetterLC && $containsDigit && $containsSpecial) ) {
$this->form_validation->set_message('password_check', '{field} must contain UPPERCASE and lowercase letters, digits, and special characters.');
return FALSE;
}
return TRUE;
}
The method should return FALSE, but as long as required is before my custom rule and the field is empty, it should stop there with Required field message, NOT the custom method message.
Okay guys, I've managed to solve it by extending the Form_validation library, putting my callback method there and piping as the other rules (without callback_ prefix).
Unfortunately, as described in the code from CI, callbacks validation rules are always verified first, prior to ‘required’ for instance.
There is an official issue opened at CI : https://github.com/bcit-ci/CodeIgniter/issues/5077

AMFPHP overiding default function arguments?

I've got this odd problem.
If I make a call on that function through amfphp service browser and give it a valid ID and leave the $num_images field blank amfphp will actually pass a blank string as the argument.
// if i call this function width just an ID
function getWorkers($id, $num_images = 100) {
...
// num_images will be set as ''
}
I can easily override using a check:
function getWorkers($id, $num_images = 100) {
if($num_images=='') $num_images = 100;
...
// num_images will now be really set as 100
}
Anyone experiencing the same with amfphp?
That's odd, I never got that from AMFPHP. If you don't have the latest version try updating your installation of AMFPHP. Also make sure Flash doesn't somehow pass an empty variable as the second variable.
(Copied from the comment.)