General developer forum

Archetype role - Adobe Connect - PHPUnit

 
Picture of Aurélien Besson
Archetype role - Adobe Connect - PHPUnit
 

I'am in trouble with unity test and archetype and Adobe Connect , userlib_test.php return error because it do not accep archetype empty in role table.

See informations below :

1 - Creating role in Moodle

in lib/accesslib.php

/**
 * Function that creates a role
 *
 * @param string $name role name
 * @param string $shortname role short name
 * @param string $description role description
 * @param string $archetype
 * @return int id or dml_exception
 */
function create_role($name, $shortname, $description, $archetype = '') 
[.....]
// verify role archetype actually exists
$archetypes = get_role_archetypes();
    if (empty($archetypes[$archetype])) {
        $archetype = '';
    }

 

  • archetypes are harcoded and returned by get_role_archetypes(), i propose to modify comment to help developpers:

@param string $archetype - one of an archetype existing in system see // get_role_archetypes()


2 - Adobe Connect - creating role

in /mod/adobeconnect/db/install.php

if ($rid = create_role(get_string('adobeconnectpresenter', 'adobeconnect'), 'adobeconnectpresenter', get_string('adobeconnectpresenterdescription', 'adobeconnect'), 'adobeconnectpresenter')) {
$mrole = new stdClass();
    $mrole->id = $rid;
    $result = $result && assign_capability('mod/adobeconnect:meetingpresenter', CAP_ALLOW, $mrole->id, $sysctx->id);
    set_role_contextlevels($mrole->id, $levels);
} else {
    $result = false;
}


  • plugin use create_role function but give an param (archetype) not kowns by system, in result an record with empty archetype column in role table


3 - PHPUnit

in user/tests/userlib_test.php

$allroles = $DB->get_records_menu('role', array(), 'id', 'archetype, id');  


  • i think it's wrong way, system can't return list role base on archetype because it do not accept new archetype :
  1. if it's madatory to use archetype, add function to clean result to delete empty entries cause by empty archetypes

  2. or modify $allrole to use shortname instead of archetypes to cover all role in system including new and 8 hardcoded 
$allroles = $DB->get_records_menu('role', array(), 'id', 'shortname, id');


This is two ways to get better unity test, i think because i my first months with Moodle.  I'am not sure if my proposition is good, any ideas ?

 
Average of ratings: -
Renaat
Re: Archetype role - Adobe Connect - PHPUnit
Core developersParticularly helpful MoodlersPlugin developers

PHPUnit uses other tables (or a different server). So first you have to add the role in your tests:

$id = create_role('adobeconnectpresenter', 'adobeconnectpresenter', 'presenter description', 'archetype');

Then you need to manually add usernames and passwords to get it working.

 
Average of ratings: -
Picture of Aurélien Besson
Re: Archetype role - Adobe Connect - PHPUnit
 

I kown that, but the problem it's with this code 


3 - PHPUnit

in user/tests/userlib_test.php

$allroles = $DB->get_records_menu('role', array(), 'id', 'archetype, id');  

$allroles contains :

[manager] => 1,
[coursecreator] => 2,
[editingteacher] => 3,
[teacher]=> 4,
[student] => 5,
[guest] => 6,
[user] => 7,
[frontpage] => 8,
[]=> 11


As Adobe Conncet add 3 new roles with no archetype (because the system do not accept new archetype), the result contain an empty key with value 11 (8 archtype + 3 new empty entries provide by Adobe Connect) and this is cause an error.

 
Average of ratings: -
Picture of Andrew Nicols
Re: Archetype role - Adobe Connect - PHPUnit
Core developersMoodle HQParticularly helpful MoodlersPlugin developersTesters

Hello,

It is actually perfectly legitimate to create a role without any Archetype.

All this means is that the role will have no capabilities assigned to them by default, and when new capabilities are added, they obviously won't be assigned by default.

There are several reasons to create roles without an archetype - it allows you to create supplemental roles for various scenarios. For example, you my create a 'Moderator' role which adds certain forum-specific capabilities and which you can add to an existing user of any type at the course context, and they will only gain the relevant capabilities without the ability to do something like Grade.

It is also completely legitimate to create multiple roles of the same archetype.

That all leads to the main issue at hand - you are creating an array with the archetype as a key, and a single role id as the value.

Given the archetype is optional, and you can have multiple roles per archetype, you need to fetch them in an alternative way.

The main question is what are you actually trying to do, because the only times you should be using archetypes are:

  1. when creating new capabilities you use the archetype to indicate which roles will have the capability by default; and
  2. when creating a new role (which typically you should not be doing except in tests).

So if you are creating new roles in your code, you are almost certainly doing it wrong (there are a couple of scenarios where you may need to do so). I'm not entirely sure what your code example in your first post is intended to do. It looks like you're trying to create a role and assign capabilities by default. Is there not an existing role archetype that you can assign these to by default (e.g. Manager)?

Hope that helps,

Andrew

 
Average of ratings: -
Picture of Andrew Nicols
Re: Archetype role - Adobe Connect - PHPUnit
Core developersMoodle HQParticularly helpful MoodlersPlugin developersTesters

I should also add that, where it is desirable to create a new role, you should select the shortname and id in your get_records_menu call rather than the archetype.


The shortname is unique to each role.


Also, you cannot specify a custom/unknown archetype.

 
Average of ratings: -
Picture of Aurélien Besson
Re: Archetype role - Adobe Connect - PHPUnit
 

In fact this code is core and i think it's wrong because it cause an error in the php unit test with Adobe Connect plugin

user/tests/userlib_test.php

$allroles = $DB->get_records_menu('role', array(), 'id', 'archetype, id'); 


I run the php unit test in my environment and I only show an error in the code at the unit test level. So i just want to discuss about my proposition to modify this code to :

$allroles = $DB->get_records_menu('role', array(), 'id', 'shortname, id'); 


 
Average of ratings: -
Picture of Aurélien Besson
Re: Archetype role - Adobe Connect - PHPUnit
 

I created a Moodle Tracker  see https://tracker.moodle.org/browse/MDL-63842 and the line cause this error has been removed because is not use in the function.


 
Average of ratings: -