_smarty = new Sugar_Smarty; } public function providerCustomCode() { $onclick = 'this.form.module.value=\'Contacts\';'; $expected_onclick = 'var _form = document.getElementById(\'DetailView\');_form.module.value=\'Contacts\';_form.submit();'; $onclick2 = 'this.form.module.value=\'Projects\';'; $expected_onclick2 = 'var _form = document.getElementById(\'DetailView\');_form.module.value=\'Projects\';_form.submit();'; $onclick3 = 'this.form.module.value=\'Meeting\';'; $expected_onclick3 = 'var _form = document.getElementById(\'DetailView\');_form.module.value=\'Meeting\';_form.submit();'; $onclick4 = 'this.form.module.value=\'Notes\';'; $expected_onclick4 = 'var _form = document.getElementById(\'DetailView\');_form.module.value=\'Notes\';_form.submit();'; return array( //set #0: simple input code array( '', array( 'tag' => 'input', 'type' => 'submit', 'self_closing' => true, 'onclick' => $onclick, 'value' => '{$APP.BUTTON_LABEL}' ), '', ), //set #1: array( '', array( 'tag' => 'input', 'type' => 'submit', 'self_closing' => true, 'onclick' => $onclick, 'value' => '{$APP.BUTTON_LABEL}', 'disabled' => '', ), '', ), //set #2: custom code contains smarty conditional statement array( '', array( 'tag' => 'input', 'type' => 'submit', 'self_closing' => true, 'onclick' => '{if $bean->access(\'edit\')}'.$onclick.'{/if}', 'value' => '{$APP.BUTTON_LABEL}' ), '', ), //set #3: attributes wrapped with smarty array( //custom code 'access(\'edit\')}type="submit"{else}type="hidden"{/if} value="{$APP.BUTTON_LABEL}" {if $bean->access(\'edit\')}onclick="'.$onclick.'"{else}onclick="alert(\'nope\');"{/if} id="button_submit">', //parsed array array( 'id' => 'button_submit', 'tag' => 'input', 'self_closing' => true, 'smarty' => array( array( 'template' => '{if $bean->access(\'edit\')}[CONTENT0]{else}[CONTENT1]{/if}', '[CONTENT0]' => array( 'type' => 'submit' ), '[CONTENT1]' => array( 'type' => 'hidden' ) ), array( 'template' => '{if $bean->access(\'edit\')}[CONTENT0]{else}[CONTENT1]{/if}', '[CONTENT0]' => array( 'onclick' => $onclick ), '[CONTENT1]' => array( 'onclick' => 'alert(\'nope\');' ) ) ), 'value' => '{$APP.BUTTON_LABEL}' ), 'access(\'edit\')}type="submit"{else}type="hidden"{/if} {if $bean->access(\'edit\')}onclick="'.$expected_onclick.'"{else}onclick="alert(\'nope\');"{/if} value="{$APP.BUTTON_LABEL}" id="button_submit"/>', ), //set #4: attributes wrapped with smarty array( //custom code 'access(\'edit\')}onclick="'.$onclick.'"{else}onclick="this.form.module.value=\'{$APP.MODULE}\';this.form.action.value=\'{$APP.ACTION}\';"{/if} value="{$APP.BUTTON_LABEL}" id="button_submit">', //parsed array array( 'id' => 'button_submit', 'tag' => 'input', 'type' => 'submit', 'self_closing' => true, 'smarty' => array( array( 'template' => '{if $bean->access(\'edit\')}[CONTENT0]{else}[CONTENT1]{/if}', '[CONTENT0]' => array( 'onclick' => $onclick ), '[CONTENT1]' => array( 'onclick' => 'this.form.module.value=\'{$APP.MODULE}\';this.form.action.value=\'{$APP.ACTION}\';' ) ) ), 'value' => '{$APP.BUTTON_LABEL}' ), 'access(\'edit\')}onclick="'.$expected_onclick.'"{else}onclick="var _form = document.getElementById(\'DetailView\');_form.module.value=\'{$APP.MODULE}\';_form.action.value=\'{$APP.ACTION}\';_form.submit();"{/if} type="button" value="{$APP.BUTTON_LABEL}" id="button_submit"/>', ), //set #5: recursive smarty wrapper within the attributes array( 'access(\'edit\')}{if $APP.CONTAINER = true}onclick="'.$onclick.'"{/if}{else if $bean->access(\'delete\') }onclick="del();"{else}onclick="alert(\'nope\');"{/if} id="button_submit">', array( 'id' => 'button_submit', 'tag' => 'input', 'type' => 'submit', 'self_closing' => true, 'value' => '{$APP.BUTTON_LABEL}', 'smarty' => array( array( 'template' => '{$APP.DISABLED}' ), array( 'template' => '{if $bean->access(\'edit\')}[CONTENT0]{else if $bean->access(\'delete\') }[CONTENT1]{else}[CONTENT2]{/if}', '[CONTENT0]' => array( 'smarty' => array( array( 'template' => '{if $APP.CONTAINER = true}[CONTENT0]{/if}', '[CONTENT0]' => array( 'onclick' => $onclick ), ) ), ), '[CONTENT1]' => array( 'onclick' => 'del();' ), '[CONTENT2]' => array( 'onclick' => 'alert(\'nope\');' ), ), ), ), 'access(\'edit\')}{if $APP.CONTAINER = true}onclick="'.$expected_onclick.'"{/if}{else if $bean->access(\'delete\') }onclick="del();"{else}onclick="alert(\'nope\');"{/if} type="button" value="{$APP.BUTTON_LABEL}" id="button_submit"/>', ), //set #6: Begins with smarty conditional statement array( '{if $fields.status.value != "Held"} {/if}', array( 'smarty' => array( array( 'template' => '{if $fields.status.value != "Held"}[CONTENT0]{/if}', '[CONTENT0]' => array( 'tag' => 'input', 'type' => 'submit', 'self_closing' => true, 'title' => '{$APP.LBL_CLOSE_AND_CREATE_BUTTON_TITLE}', 'class' => 'button', 'onclick' => $onclick, 'value' => '{$APP.LBL_CLOSE_AND_CREATE_BUTTON_TITLE}' ), ) ), ), '{if $fields.status.value != "Held"}{/if}' ), //set #7: Begins with smarty conditional statement and contains recursive conditional statement inside the context array( '{ if($fields.status.value != "Held") } access(\'edit\')}{if $APP.CONTAINER = true}onclick="'.$onclick.'"{/if}{else if $bean->access(\'delete\') }onclick="del();"{else}onclick="alert(\'nope\');"{/if} class="button" onclick="'.$onclick.'" value="{$APP.LBL_CLOSE_AND_CREATE_BUTTON_TITLE}" type="submit">{/if}', array( 'smarty' => array( array( 'template' => '{ if($fields.status.value != "Held") }[CONTENT0]{/if}', '[CONTENT0]' => array( 'tag' => 'input', 'type' => 'submit', 'class' => 'button', 'value' => '{$APP.LBL_CLOSE_AND_CREATE_BUTTON_TITLE}', 'onclick' => $onclick, 'self_closing' => true, 'title' => '{$APP.LBL_CLOSE_AND_CREATE_BUTTON_TITLE}', 'smarty' => array( array( 'template' => '{if $bean->access(\'edit\')}[CONTENT0]{else if $bean->access(\'delete\') }[CONTENT1]{else}[CONTENT2]{/if}', '[CONTENT0]' => array( 'smarty' => array( array( 'template' => '{if $APP.CONTAINER = true}[CONTENT0]{/if}', '[CONTENT0]' => array( 'onclick' => $onclick ), ) ), ), '[CONTENT1]' => array( 'onclick' => 'del();' ), '[CONTENT2]' => array( 'onclick' => 'alert(\'nope\');' ), ) ), ), ) ), ), '{ if($fields.status.value != "Held") }access(\'edit\')}{if $APP.CONTAINER = true}onclick="'.$expected_onclick.'"{/if}{else if $bean->access(\'delete\') }onclick="del();"{else}onclick="alert(\'nope\');"{/if} title="{$APP.LBL_CLOSE_AND_CREATE_BUTTON_TITLE}" class="button" onclick="'.$expected_onclick.'" value="{$APP.LBL_CLOSE_AND_CREATE_BUTTON_TITLE}" type="button"/>{/if}' ), //set #8: The submit button is encapsulated with another form array( '
', array( 'tag' => 'form', 'name' => 'blah', 'self_closing' => false, 'container' => array( array( 'tag' => 'input', 'type' => 'hidden', 'name' => 'id1', 'self_closing' => true, ), array( 'tag' => 'input', 'type' => 'hidden', 'name' => 'id2', 'self_closing' => true, ), array( 'tag' => 'input', 'type' => 'submit', 'onclick' => $onclick, 'self_closing' => true, ), ), ), '
', ), //set #9: custom code encapsulated smarty conditional statement, and contains additional hidden fields array( '{if $fields.status.value != "Held"} {/if}', array( 'smarty' => array( array( 'template' => '{if $fields.status.value != "Held"}[CONTENT0]{/if}', '[CONTENT0]' => array( array( 'tag' => 'input', 'type' => 'hidden', 'name' => 'id1', 'self_closing' => true, 'value' => "true" ), array( 'tag' => 'input', 'type' => 'hidden', 'name' => 'id2', 'self_closing' => true, ), array( 'tag' => 'input', 'type' => 'submit', 'value' => '{$APP.LBL_CLOSE_AND_CREATE_BUTTON_TITLE}', 'self_closing' => true, 'smarty' => array( array( 'template' => '{if $APP.CONTAINER = true}[CONTENT0]{else}[CONTENT1]{/if}', '[CONTENT0]' => array( 'onclick' => $onclick ), '[CONTENT1]' => array( 'onclick' => 'stop();' ), ) ), ), ), ) ), ), '{if $fields.status.value != "Held"}{/if}' ), //set #10: empty spaces after the equal sign array ( '', array( 'tag' => 'input', 'title' => '{$APP.LBL_SAVE_BUTTON_TITLE}', 'id' => 'SAVE', 'disabled' => '', 'onclick' => 'SUGAR.meetings.fill_invitees();document.EditView.action.value=\'Save\'; document.EditView.return_action.value=\'DetailView\'; {if isset($smarty.request.isDuplicate) && $smarty.request.isDuplicate eq "true"}document.EditView.return_id.value=\'\'; {/if} formSubmitCheck();', 'type' => 'button', 'name' => "button", 'value' => '{$APP.LBL_SAVE_BUTTON_LABEL}', 'self_closing' => true, ), '', ), //set #11: empty spaces before the equal sign array ( '', array( 'tag' => 'input', 'title' => '{$APP.LBL_SAVE_BUTTON_TITLE}', 'id' => 'SAVE', 'disabled' => '', 'onclick' => 'SUGAR.meetings.fill_invitees();document.EditView.action.value=\'Save\'; document.EditView.return_action.value=\'DetailView\'; {if isset($smarty.request.isDuplicate) && $smarty.request.isDuplicate eq "true"}document.EditView.return_id.value=\'\'; {/if} formSubmitCheck();', 'type' => 'button', 'name' => "button", 'value' => '{$APP.LBL_SAVE_BUTTON_LABEL}', 'self_closing' => true, ), '', ), //set #12: Contains smarty syntax "ldelim, rdelim" array( '{if $bean->aclAccess("delete") && !empty($smarty.request.record)}{/if} ', array( 'smarty' => array( array( 'template' => '{if $bean->aclAccess("delete") && !empty($smarty.request.record)}[CONTENT0]{/if}', '[CONTENT0]' => array( 'tag' => 'input', 'self_closing' => true, 'title' => '{$APP.LBL_DELETE_BUTTON_TITLE}', 'accessKey' => '{$APP.LBL_DELETE_BUTTON_KEY}', 'class' => 'button', 'onclick' => 'this.form.return_module.value=\'Users\'; this.form.return_action.value=\'EditView\'; this.form.action.value=\'Delete\'; this.form.return_id.value=\'{$return_id}\'; if (confirm(\'{$APP.NTC_DELETE_CONFIRMATION}\')){ldelim}disableOnUnloadEditView(); return true;{rdelim}else{ldelim}return false;{rdelim};', 'type' => 'submit', 'name' => 'Delete', 'value' => '{$APP.LBL_DELETE_BUTTON_LABEL}', ), ) ) ), '{if $bean->aclAccess("delete") && !empty($smarty.request.record)}{/if}' ), //set #13: Contains smarty syntax "literal" array( '{if $bean->aclAccess("delete") && !empty($smarty.request.record)}{/if} ', array( 'smarty' => array( array( 'template' => '{if $bean->aclAccess("delete") && !empty($smarty.request.record)}[CONTENT0]{/if}', '[CONTENT0]' => array( 'tag' => 'input', 'self_closing' => true, 'title' => '{$APP.LBL_DELETE_BUTTON_TITLE}', 'accessKey' => '{$APP.LBL_DELETE_BUTTON_KEY}', 'class' => 'button', 'onclick' => 'this.form.return_module.value=\'Users\'; this.form.return_action.value=\'EditView\'; this.form.action.value=\'Delete\'; this.form.return_id.value=\'{$return_id}\'; {literal}if (confirm(\'{$APP.NTC_DELETE_CONFIRMATION}\')){disableOnUnloadEditView(); return true;}else{return false;};{/literal}', 'type' => 'submit', 'name' => 'Delete', 'value' => '{$APP.LBL_DELETE_BUTTON_LABEL}', ), ) ) ), '{if $bean->aclAccess("delete") && !empty($smarty.request.record)}{/if}' ), //set #14: Multiple conditional statement array( '{if !empty($smarty.request.return_action) && $smarty.request.return_action == "ProjectTemplatesDetailView" && (!empty($fields.id.value) || !empty($smarty.request.return_id)) }'. ' '. '{elseif !empty($smarty.request.return_action) && $smarty.request.return_action == "DetailView" && (!empty($fields.id.value) || !empty($smarty.request.return_id)) }'. ' '. '{elseif $is_template}'. ' '. '{else}'. ' '. '{/if}', array( 'smarty' => array( array( 'template' => '{if !empty($smarty.request.return_action) && $smarty.request.return_action == "ProjectTemplatesDetailView" && (!empty($fields.id.value) || !empty($smarty.request.return_id)) }[CONTENT0]{elseif !empty($smarty.request.return_action) && $smarty.request.return_action == "DetailView" && (!empty($fields.id.value) || !empty($smarty.request.return_id)) }[CONTENT1]{elseif $is_template}[CONTENT2]{else}[CONTENT3]{/if}', '[CONTENT0]' => array( 'tag' => 'input', 'title' => '{$APP.LBL_CANCEL_BUTTON_TITLE}', 'accessKey' => '{$APP.LBL_CANCEL_BUTTON_KEY}', 'class' => "button", 'type' => "submit", 'name' => "button", 'value' => '{$APP.LBL_CANCEL_BUTTON_LABEL}', 'id' => "Cancel", 'onclick' => $onclick, 'self_closing' => true ), '[CONTENT1]' => array( 'tag' => 'input', 'title' => '{$APP.LBL_CANCEL_BUTTON_TITLE}', 'accessKey' => '{$APP.LBL_CANCEL_BUTTON_KEY}', 'class' => "button", 'type' => "submit", 'name' => "button", 'value' => '{$APP.LBL_CANCEL_BUTTON_LABEL}', 'id' => "Cancel", 'onclick' => $onclick2, 'self_closing' => true ), '[CONTENT2]' => array( 'tag' => 'input', 'title' => '{$APP.LBL_CANCEL_BUTTON_TITLE}', 'accessKey' => '{$APP.LBL_CANCEL_BUTTON_KEY}', 'class' => "button", 'type' => "submit", 'name' => "button", 'value' => '{$APP.LBL_CANCEL_BUTTON_LABEL}', 'id' => "Cancel", 'onclick' => $onclick3, 'self_closing' => true ), '[CONTENT3]' => array( 'tag' => 'input', 'title' => '{$APP.LBL_CANCEL_BUTTON_TITLE}', 'accessKey' => '{$APP.LBL_CANCEL_BUTTON_KEY}', 'class' => "button", 'type' => "submit", 'name' => "button", 'value' => '{$APP.LBL_CANCEL_BUTTON_LABEL}', 'id' => "Cancel", 'onclick' => $onclick4, 'self_closing' => true ), ) ) ), '{if !empty($smarty.request.return_action) && $smarty.request.return_action == "ProjectTemplatesDetailView" && (!empty($fields.id.value) || !empty($smarty.request.return_id)) }'. ''. '{elseif !empty($smarty.request.return_action) && $smarty.request.return_action == "DetailView" && (!empty($fields.id.value) || !empty($smarty.request.return_id)) }'. ''. '{elseif $is_template}'. ''. '{else}'. ''. '{/if}' ), //set #15: Parallel smarty strings array( '{$APP.VALUE1} {$HIDDEN_FIELD} ', array( array( 'smarty' => array( array( 'template' => '{$APP.VALUE1}', ) ), ), array( 'smarty' => array( array( 'template' => '{$HIDDEN_FIELD}', ) ), ), array( 'tag' => 'input', 'type' => 'submit', 'self_closing' => true, 'onclick' => $onclick, 'value' => '{$APP.BUTTON_LABEL}' ) ), '{$APP.VALUE1}{$HIDDEN_FIELD}', ), //set #16: Contains smarty syntax "nocache" array( '
' .'{nocache}' .'{sugar_email_btn}' .'{/nocache}' .'
', array( 'tag' => 'form', 'action' => 'index.php', 'method' => '{$PDFMETHOD}', 'name' => 'ViewPDF', 'id' => 'form', 'onsubmit' => 'this.sugarpdf.value =(document.getElementById(\'sugarpdf\'))? document.getElementById(\'sugarpdf\').value: \'\';', 'container' => array( array( 'tag' => 'input', 'type' => "hidden", 'name' => "module", 'value' => "Quotes", 'self_closing' => true, ), array( 'smarty' => array( array( 'template' => '{nocache}{sugar_email_btn}{/nocache}', ), ) ) ), 'self_closing' => false, ), '
' .'{nocache}' .'{sugar_email_btn}' .'{/nocache}' .'
', ), ); } /** * @dataProvider providerCustomCode */ public function testCustomCode($customCode, $expected_parsed_array, $expected_customCode) { //Test for parseHtmlTag $this->assertEquals($expected_parsed_array, SugarHtml::parseHtmlTag($customCode)); $params = array( 'module' => 'Accounts', 'view' => 'DetailView', 'id' => array( 'customCode' => $customCode ), 'form_id' => 'DetailView' ); //Test for smarty_function_sugar_button for customCode $this->assertEquals($expected_customCode, smarty_function_sugar_button($params, $this->_smarty)); } public function providerCustomCodeWithHidden() { $onclick = 'this.form.module.value=\'Contacts\';this.form.action.value=\'DetailView\';'; $expected_onclick = 'var _form = document.getElementById(\'DetailView\');_form.module.value=\'Contacts\';_form.action.value=\'DetailView\';_form.submit();'; return array( //set #0: Button with hidden field array( ' ', '', array( '' ), ), //set #1: Button with hidden field wrapping with conditional smarty statement array( '{if $fields.status.value != "Held"} {/if}', '{if $fields.status.value != "Held"}{/if}', array( '{if $fields.status.value != "Held"}{/if}', ), ), //set #2: wrapping with conditional smarty statement great equal than two phases array( '{if $fields.status.value != "Held"}{else}{/if}', '{if $fields.status.value != "Held"}{else}{/if}', array( '{if $fields.status.value != "Held"}{else}{/if}', ), ), //set #3: hidden fields wrapped with the additional form element array( '
', '
', null ), ); } /** * @dataProvider providerCustomCodeWithHidden */ public function testCustomCodeWithHidden($customCode, $expected_customCode, $expected_hidden_array) { $params = array( 'module' => 'Accounts', 'view' => 'DetailView', 'id' => array( 'customCode' => $customCode ), 'form_id' => 'DetailView' ); $this->assertEquals($expected_customCode, smarty_function_sugar_button($params, $this->_smarty)); $form = $this->_smarty->get_template_vars('form'); $this->assertEquals($expected_hidden_array, $form['hidden']); } public function testBuildSugarHtml() { $sugar_html = array( 'type' => 'submit', 'value' => '{$APP.LBL_CLOSE_AND_CREATE_BUTTON_TITLE}', 'htmlOptions' => array( 'title' => '{$APP.LBL_CLOSE_AND_CREATE_BUTTON_TITLE}', 'name' => 'button', 'class' => 'button', 'onclick' => 'this.form.isSaveFromDetailView.value=true; this.form.status.value=\'Held\'; this.form.action.value=\'Save\';this.form.return_module.value=\'Meetings\';this.form.isDuplicate.value=true;this.form.isSaveAndNew.value=true;this.form.return_action.value=\'EditView\'; this.form.isDuplicate.value=true;this.form.return_id.value=\'{$fields.id.value}\';', ), 'template' => '{if $fields.status.value != "Held"}[CONTENT]{/if}', ); $expected_html = '{if $fields.status.value != "Held"}{/if}'; $params = array( 'module' => 'Accounts', 'view' => 'DetailView', 'id' => array( 'sugar_html' => $sugar_html ), 'form_id' => 'DetailView' ); //Test for smarty_function_sugar_button for sugar_html $this->assertEquals($expected_html, smarty_function_sugar_button($params, $this->_smarty)); } }