You are here

function CourseContextTestCase::testMultiContext in Course 7.2

Same name and namespace in other branches
  1. 6 tests/CourseContextTestCase.test \CourseContextTestCase::testMultiContext()
  2. 7 tests/CourseContextTestCase.test \CourseContextTestCase::testMultiContext()

Test objects that belong to multiple courses.

File

tests/CourseContextTestCase.test, line 39

Class

CourseContextTestCase
Tests for Course context

Code

function testMultiContext() {
  $courseNode1 = $this
    ->createCourseNode();
  $courseNode2 = $this
    ->createCourseNode();

  // Create an object and give it an instance.
  $co1 = $this
    ->createCourseObject($courseNode1);
  $co1
    ->setInstanceId(1234)
    ->save();
  $co2 = $this
    ->createCourseObject($courseNode2);
  $co2
    ->setInstanceId(1234)
    ->save();

  // Now we have 2 course objects with the same instance but in different courses.
  // Simulate us taking one of the objects, and switch back and forth between courses.
  $co1
    ->takeCourseObject();
  $foundCourseNode1 = course_determine_context('course_test', 'course_test_object', 1234);
  $this
    ->assertEqual($courseNode1->nid, $foundCourseNode1->nid, 'Found the right course context, pass 1.');

  // Because this is a unit test the static cache is on. We have to flush it.
  drupal_static_reset('course_determine_context');

  // Taking with context set.
  $co2
    ->takeCourseObject();
  $foundCourseNode2 = course_determine_context('course_test', 'course_test_object', 1234);
  $this
    ->assertEqual($courseNode2->nid, $foundCourseNode2->nid, 'Found the right course context, pass 2.');

  // Because this is a unit test the static cache is on. We have to flush it.
  drupal_static_reset('course_determine_context');

  // Back to original context.
  $co1
    ->takeCourseObject();
  $foundCourseNode1 = course_determine_context('course_test', 'course_test_object', 1234);
  $this
    ->assertEqual($courseNode1->nid, $foundCourseNode1->nid, 'Found the right course context, pass 3.');

  // Because this is a unit test the static cache is on. We have to flush it.
  drupal_static_reset('course_determine_context');

  // Simulate a cold context check.
  unset($_SESSION['course']);
  $foundCourseNode1 = course_determine_context('course_test', 'course_test_object', 1234);
  $this
    ->assertEqual($courseNode1->nid, $foundCourseNode1->nid, 'Found the right course context, pass 4.');
}