3333
3434namespace GlpiPlugin \Advancedforms \Tests \Model \Mapper ;
3535
36+ use AuthLDAP ;
3637use Glpi \Form \AccessControl \FormAccessControlManager ;
3738use Glpi \Form \Migration \FormMigration ;
3839use Glpi \Form \Question ;
39- use GlpiPlugin \Advancedforms \Model \QuestionType \IpAddressQuestion ;
4040use GlpiPlugin \Advancedforms \Model \QuestionType \LdapQuestion ;
4141use GlpiPlugin \Advancedforms \Model \QuestionType \LdapQuestionConfig ;
4242use LogicException ;
4343use RuntimeException ;
4444
4545final class FormcreatorLdapSelectTypeMapperTest extends MapperTestCase
4646{
47- public function testIpTypeMigrationWhenEnabled (): void
47+ public function testLdapSelectTypeMigrationWhenEnabledWithValidAuthLDAP (): void
4848 {
4949 /** @var \DBmysql $DB */
5050 global $ DB ;
5151
52- // Arrange: enable ip question type and add some fomrcreator data
52+ // Arrange: create a valid AuthLDAP
53+ $ authldap = $ this ->createItem (AuthLDAP::class, [
54+ 'name ' => 'My LDAP server ' ,
55+ 'is_active ' => 1 ,
56+ ]);
57+
58+ // Act & Assert: test migration with the valid AuthLDAP
59+ $ this ->testLdapSelectTypeMigrationWhenEnabled (
60+ authldap_id: $ authldap ->getId (),
61+ expected_authldap_id: null ,
62+ );
63+ }
64+
65+ public function testLdapSelectTypeMigrationWhenEnabledWithInvalidAuthLDAP (): void
66+ {
67+ // Act & Assert: test migration with an invalid AuthLDAP
68+ $ this ->testLdapSelectTypeMigrationWhenEnabled (
69+ authldap_id: 123 ,
70+ expected_authldap_id: 0 ,
71+ );
72+ }
73+
74+ private function testLdapSelectTypeMigrationWhenEnabled (int $ authldap_id , ?int $ expected_authldap_id ): void
75+ {
76+ /** @var \DBmysql $DB */
77+ global $ DB ;
78+
79+ // Arrange: enable ldap select question type and add some formcreator data
5380 $ this ->enableConfigurableItem (LdapQuestion::class);
5481 $ this ->createSimpleFormcreatorForm (
5582 name: "My form " ,
5683 questions: [
5784 [
5885 'name ' => 'My LDAP question ' ,
5986 'fieldtype ' => 'ldapselect ' ,
60- 'values ' => '{"ldap_auth":"123 ","ldap_attribute":"456","ldap_filter":"(& (uid=*) (objectClass=inetOrgPerson))"} ' ,
87+ 'values ' => '{"ldap_auth":" ' . $ authldap_id . ' ","ldap_attribute":"456","ldap_filter":"(& (uid=*) (objectClass=inetOrgPerson))"} ' ,
6188 ],
6289 ],
6390 );
@@ -80,27 +107,31 @@ public function testIpTypeMigrationWhenEnabled(): void
80107 if (!$ config instanceof LdapQuestionConfig) {
81108 throw new LogicException ();
82109 }
83- $ this ->assertEquals (123 , $ config ->getAuthLdapId ());
110+ $ this ->assertEquals ($ expected_authldap_id ?? $ authldap_id , $ config ->getAuthLdapId ());
84111 $ this ->assertEquals (456 , $ config ->getLdapAttributeId ());
85112 $ this ->assertEquals (
86113 "(& (uid=*) (objectClass=inetOrgPerson)) " ,
87114 $ config ->getLdapFilter (),
88115 );
89116 }
90117
91- public function testIpTypeMigrationWhenDisabled (): void
118+ public function testLdapSelectTypeMigrationWhenDisabled (): void
92119 {
93120 /** @var \DBmysql $DB */
94121 global $ DB ;
95122
96- // Arrange: add some fomrcreator data
123+ // Arrange: add some formcreator data
124+ $ authldap = $ this ->createItem (AuthLDAP::class, [
125+ 'name ' => 'My LDAP server ' ,
126+ 'is_active ' => 1 ,
127+ ]);
97128 $ this ->createSimpleFormcreatorForm (
98129 name: "My form " ,
99130 questions: [
100131 [
101132 'name ' => 'My LDAP question ' ,
102133 'fieldtype ' => 'ldapselect ' ,
103- 'values ' => '{"ldap_auth":"123 ","ldap_attribute":"456","ldap_filter":"(& (uid=*) (objectClass=inetOrgPerson))"} ' ,
134+ 'values ' => '{"ldap_auth":" ' . $ authldap -> getId () . ' ","ldap_attribute":"456","ldap_filter":"(& (uid=*) (objectClass=inetOrgPerson))"} ' ,
104135 ],
105136 ],
106137 );
0 commit comments