Skip to content

Commit

Permalink
Dev #17241: Create unit-test for question attribute event plugin (#1848)
Browse files Browse the repository at this point in the history
Co-authored-by: encuestabizdevgit <devgit@encuesta.biz>
  • Loading branch information
gabrieljenik and encuestabizdevgit committed Apr 16, 2021
1 parent 34cb173 commit 794422b
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 0 deletions.
30 changes: 30 additions & 0 deletions tests/data/plugins/NewQuestionAttributesPlugin.php
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);
}
}
116 changes: 116 additions & 0 deletions tests/functional/backend/QuestionAttributeTest.php
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();
}

}

0 comments on commit 794422b

Please sign in to comment.