@@ -491,4 +491,115 @@ private function getNextWorkingDayDate(array $working_days, $reference_date, $fo
491491
492492 return date ($ format , $ time );
493493 }
494+
495+ protected function createItemProvider ()
496+ {
497+ $ target_class = $ this ->getChildClass ()::getConcreteClass ();
498+
499+ $ tech_id = getItemByTypeName (\User::class, 'tech ' , true );
500+
501+ // Create a 7d/7 24h/24 calendar
502+ $ calendar = $ this ->createItem (\Calendar::class, ['name ' => 'testCreateItem calendar ' ]);
503+ for ($ day = 0 ; $ day <= 6 ; $ day ++) {
504+ $ this ->createItem (
505+ \CalendarSegment::class,
506+ [
507+ 'calendars_id ' => $ calendar ->getID (),
508+ 'day ' => $ day ,
509+ 'begin ' => '00:00:00 ' ,
510+ 'end ' => '24:00:00 '
511+ ]
512+ );
513+ }
514+
515+ // Status is computed to INCOMING as long as there is no predefined assignee
516+ yield [
517+ 'predefined_fields ' => [
518+ '1 ' => 'Test ' , // 1=name
519+ ],
520+ 'expected_fields ' => [
521+ 'name ' => 'Test ' ,
522+ 'status ' => \CommonITILObject::INCOMING ,
523+ ],
524+ ];
525+
526+ // Status is computed to ASSIGNED as long as there is a predefined assignee (except if ASSIGNED is not valid status)
527+ yield [
528+ 'predefined_fields ' => [
529+ '5 ' => $ tech_id ,
530+ ],
531+ 'expected_fields ' => [
532+ 'status ' => in_array (\CommonITILObject::ASSIGNED , array_keys ($ target_class ::getAllStatusArray ()))
533+ ? \CommonITILObject::ASSIGNED
534+ : \CommonITILObject::INCOMING ,
535+ ],
536+ ];
537+
538+ // Predefined status is preserved
539+ yield [
540+ 'predefined_fields ' => [
541+ '5 ' => $ tech_id ,
542+ '12 ' => \CommonITILObject::INCOMING , // 12=status
543+ ],
544+ 'expected_fields ' => [
545+ 'status ' => \CommonITILObject::INCOMING ,
546+ ],
547+ ];
548+ }
549+
550+ /**
551+ * @param array $predefined_fields
552+ * @param array $expected_fields
553+ *
554+ * @dataProvider createItemProvider
555+ */
556+ public function testCreateItem (array $ predefined_fields , array $ expected_fields )
557+ {
558+ $ child_class = $ this ->getChildClass ();
559+ $ template_class = $ child_class ::getTemplateClass ();
560+ $ predefined_fields_class = $ child_class ::getPredefinedFieldsClass ();
561+
562+ $ calendar = getItemByTypeName (\Calendar::class, 'testCreateItem calendar ' );
563+
564+ // Create template and its predefined fields
565+ $ template = $ this ->createItem (
566+ $ template_class ,
567+ [
568+ 'name ' => __METHOD__
569+ ]
570+ );
571+ foreach ($ predefined_fields as $ num => $ value ) {
572+ $ this ->createItem (
573+ $ predefined_fields_class ,
574+ [
575+ $ template ->getForeignKeyField () => $ template ->getID (),
576+ 'num ' => $ num ,
577+ 'value ' => $ value ,
578+ ]
579+ );
580+ }
581+
582+ // Create item
583+ $ instance = $ this ->createItem (
584+ $ child_class ,
585+ [
586+ 'name ' => __METHOD__ ,
587+ 'begin_date ' => date ('Y-m-d H:i:s ' , strtotime ('-7 days ' )),
588+ 'end_date ' => null ,
589+ 'periodicity ' => 3600 ,
590+ 'create_before ' => 0 ,
591+ 'calendars_id ' => $ calendar ->getID (),
592+ $ template ->getForeignKeyField () => $ template ->getID (),
593+ ]
594+ );
595+ $ created_item = null ;
596+ $ this ->boolean ($ instance ->createItem ($ created_item ))->isTrue ();
597+
598+ // Validates created item fields
599+ $ this ->object ($ created_item )->isInstanceOf (\CommonITILObject::class);
600+ foreach ($ expected_fields as $ field_name => $ field_value ) {
601+ $ this ->array ($ created_item ->fields )->hasKey ($ field_name );
602+ $ this ->variable ($ created_item ->fields [$ field_name ])->isEqualTo ($ field_value );
603+ }
604+ }
494605}
0 commit comments