function CourseEnrollmentTestCase::testCourseBundles in Course 8.2
Same name and namespace in other branches
- 8.3 tests/src/Functional/CourseEnrollmentTestCase.php \Drupal\Tests\course\Functional\CourseEnrollmentTestCase::testCourseBundles()
Test course enrollment bundles.
File
- tests/
src/ Functional/ CourseEnrollmentTestCase.php, line 93
Class
- CourseEnrollmentTestCase
- Tests for Course enrollment
Namespace
Drupal\Tests\course\FunctionalCode
function testCourseBundles() {
$enrollment_type = CourseEnrollmentType::create(array(
'id' => 'type_a',
'label' => t('Bundle type A'),
));
$enrollment_type
->save();
$enrollment_type = CourseEnrollmentType::create(array(
'id' => 'type_b',
'label' => t('Bundle type B'),
));
$enrollment_type
->save();
// Add a field to course result and make it required for starting.
$field_storagea = FieldStorageConfig::create([
'id' => 'course_enrollment.enrollment_field_a',
'field_name' => 'enrollment_field_a',
'entity_type' => 'course_enrollment',
'type' => 'string',
'module' => 'core',
]);
$field_storagea
->save();
$instancea = FieldConfig::create([
'field_storage' => $field_storagea,
'bundle' => 'type_a',
'label' => 'Enrollment field A',
'required' => TRUE,
'field_name' => 'enrollment_field_a',
'entity_type' => 'course_enrollment',
'third_party_settings' => array(
'course' => [
'show_field' => TRUE,
],
),
]);
$instancea
->save();
Drupal::service('entity_display.repository')
->getFormDisplay('course_enrollment', 'type_a', 'default')
->setComponent('enrollment_field_a', array(
'type' => 'text_textfield',
))
->save();
// Add a field to course result and make it required for starting.
$field_storageb = FieldStorageConfig::create([
'id' => 'course_enrollment.enrollment_field_b',
'field_name' => 'enrollment_field_b',
'entity_type' => 'course_enrollment',
'type' => 'string',
'module' => 'core',
]);
$field_storageb
->save();
$instanceb = FieldConfig::create([
'field_storage' => $field_storageb,
'bundle' => 'type_b',
'label' => 'Enrollment field B',
'required' => TRUE,
'field_name' => 'enrollment_field_b',
'entity_type' => 'course_enrollment',
'third_party_settings' => array(
'course' => [
'show_field' => TRUE,
],
),
]);
$instanceb
->save();
Drupal::service('entity_display.repository')
->getFormDisplay('course_enrollment', 'type_b', 'default')
->setComponent('enrollment_field_b', array(
'type' => 'text_textfield',
))
->save();
$courseA = $this
->createCourse(array(
'enrollment_type' => 'type_a',
));
$courseB = $this
->createCourse(array(
'enrollment_type' => 'type_b',
));
$this
->drupalLogin($this->student_user);
// Check if field shows up and user is not yet enrolled.
$this
->drupalGet("course/{$courseA->id()}/take");
$this
->assertFieldById('edit-enrollment-field-a-0-value');
$this
->assertNoFieldById('edit-enrollment-field-b-0-value');
$enrollment = $courseA
->getEnrollment($this->student_user);
$this
->assertEmpty($enrollment);
$this
->drupalPostForm(NULL, array(), t('Save'));
// Check that form API is working.
$this
->assertText('field is required');
$this
->drupalPostForm(NULL, array(
'enrollment_field_a[0][value]' => 'test 123',
), t('Save'));
// Check that a different field is on course B
$this
->drupalGet("course/{$courseB->id()}/take");
$this
->assertFieldById('edit-enrollment-field-b-0-value');
$this
->assertNoFieldById('edit-enrollment-field-a-0-value');
// Mark field B to not show on enrollment.
$instanceb
->setThirdPartySetting('course', 'show_field', FALSE);
$instanceb
->save();
$this
->drupalGet("course/{$courseB->id()}/take");
$this
->assertNoFieldById('edit-enrollment-field-a-0-value');
$this
->assertNoFieldById('edit-enrollment-field-b-0-value');
}