-
Notifications
You must be signed in to change notification settings - Fork 994
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dev #17241: Create unit-test for question attribute event plugin (#1848)
Co-authored-by: encuestabizdevgit <devgit@encuesta.biz>
- Loading branch information
1 parent
34cb173
commit 794422b
Showing
2 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
class NewQuestionAttributesPlugin extends PluginBase | ||
{ | ||
protected static $description = 'Dummy plugin for testing newQuestionAttributes event'; | ||
protected static $name = 'NewQuestionAttributesPlugin'; | ||
|
||
public function init() | ||
{ | ||
$this->subscribe('newQuestionAttributes'); | ||
} | ||
|
||
public function newQuestionAttributes() | ||
{ | ||
$event = $this->getEvent(); | ||
$questionAttributes = [ | ||
'testAttribute' => [ | ||
'types' => 'S', | ||
'category' => gT('Test'), | ||
'sortorder' => 1, | ||
'inputtype' => 'text', | ||
'default' => '', | ||
'caption' => 'Test Attribute', | ||
'help' => 'This is a dummy attribute for testing purposes.', | ||
'expression'=> 1, | ||
], | ||
]; | ||
$event->append('questionAttributes', $questionAttributes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
namespace ls\tests; | ||
|
||
/** | ||
* @group questionattribute | ||
*/ | ||
class QuestionAttributeTest extends TestBaseClassWeb | ||
{ | ||
|
||
/** | ||
* @inheritdoc | ||
* Activate needed plugins | ||
* Import survey in tests/surveys/. | ||
*/ | ||
public static function setUpBeforeClass() | ||
{ | ||
parent::setUpBeforeClass(); | ||
|
||
require __DIR__."/../../data/plugins/NewQuestionAttributesPlugin.php"; | ||
$plugin = \Plugin::model()->findByAttributes(array('name'=>'NewQuestionAttributesPlugin')); | ||
if (!$plugin) { | ||
$plugin = new \Plugin(); | ||
$plugin->name = 'NewQuestionAttributesPlugin'; | ||
$plugin->active = 1; | ||
$plugin->save(); | ||
} else { | ||
$plugin->active = 1; | ||
$plugin->save(); | ||
} | ||
App()->getPluginManager()->loadPlugin('NewQuestionAttributesPlugin', $plugin->id); | ||
|
||
} | ||
|
||
public function testPluginAttributes() | ||
{ | ||
$questionAttributeHelper = new \LimeSurvey\Models\Services\QuestionAttributeHelper(); | ||
|
||
// Get attributes for question type 'S', which is used by the test plugin | ||
$aQuestionAttributes = $questionAttributeHelper->getAttributesFromPlugin('S'); | ||
$this->assertNotEmpty($aQuestionAttributes); | ||
|
||
// Check the test attribute exists within the attributes received | ||
$aAttribute = null; | ||
foreach ($aQuestionAttributes as $item) { | ||
if (isset($item['name']) && $item['name'] == 'testAttribute') { | ||
$aAttribute = $item; | ||
break; | ||
} | ||
} | ||
$this->assertNotEmpty($aAttribute); | ||
|
||
// Check a core attribute does NOT exist within the attributes received | ||
$aAttribute = null; | ||
foreach ($aQuestionAttributes as $item) { | ||
if (isset($item['name']) && $item['name'] == 'question_template') { | ||
$aAttribute = $item; | ||
break; | ||
} | ||
} | ||
$this->assertEmpty($aAttribute); | ||
|
||
// Get attributes for question type 'T', which is NOT used by the test plugin | ||
$aQuestionAttributes = $questionAttributeHelper->getAttributesFromPlugin('T'); | ||
|
||
// Check the test attribute does NOT exist within the attributes received | ||
$aAttribute = null; | ||
if (!empty($aQuestionAttributes)) { | ||
foreach ($aQuestionAttributes as $item) { | ||
if (isset($item['name']) && $item['name'] == 'testAttribute') { | ||
$aAttribute = $item; | ||
break; | ||
} | ||
} | ||
} | ||
$this->assertEmpty($aAttribute); | ||
} | ||
|
||
public function testCoreAttributes() | ||
{ | ||
// Get attributes for question type 'S', which is used by the test plugin | ||
$aQuestionAttributes = \QuestionAttribute::getQuestionAttributesSettings('S', true); | ||
$this->assertNotEmpty($aQuestionAttributes); | ||
|
||
// Check the test attribute (from plugin) does NOT exist within the attributes received | ||
$aAttribute = null; | ||
foreach ($aQuestionAttributes as $item) { | ||
if (isset($item['name']) && $item['name'] == 'testAttribute') { | ||
$aAttribute = $item; | ||
break; | ||
} | ||
} | ||
$this->assertEmpty($aAttribute); | ||
|
||
// Check a core attribute exists within the attributes received | ||
$aAttribute = null; | ||
foreach ($aQuestionAttributes as $item) { | ||
if (isset($item['name']) && $item['name'] == 'hide_tip') { | ||
$aAttribute = $item; | ||
break; | ||
} | ||
} | ||
$this->assertNotEmpty($aAttribute); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
* @todo Deactivate and uninstall plugins ? | ||
*/ | ||
public static function tearDownAfterClass() | ||
{ | ||
self::deActivatePlugin('NewQuestionAttributesPlugin'); | ||
parent::tearDownAfterClass(); | ||
} | ||
|
||
} |