/** * Adding this test here as get_areas_user_accesses process is the same, results just depend on the context level. * * @return void */ public function test_search_user_accesses() { global $DB, $PAGE; $area = new \core_mocksearch\search\mock_search_area(); $renderer = $PAGE->get_renderer('core_search'); $engine = new \mock_search\engine(); $course = $this->getDataGenerator()->create_course(array('fullname' => 'Course & Title')); $coursectx = context_course::instance($course->id); $user = $this->getDataGenerator()->create_user(array('firstname' => 'User', 'lastname' => 'Escape & Name')); $this->getDataGenerator()->enrol_user($user->id, $course->id, 'teacher'); // Make a record to enter in the search area. $record = new \stdClass(); $record->title = 'Escape & Title'; $record->content = 'Escape & Content'; $record->description1 = 'Escape & Description1'; $record->description2 = 'Escape & Description2'; $record->userid = $user->id; $record->courseid = $course->id; $record = $this->generator->create_record($record); // Convert to a 'doc data' type format. $docdata = $area->convert_record_to_doc_array($record); // First see that the docuemnt has the right information, unescaped. $doc = $engine->to_document($area, $docdata); $this->assertEquals('Escape & Title', $doc->get('title')); $this->assertEquals('Escape & Content', $doc->get('content')); $this->assertEquals('Escape & Description1', $doc->get('description1')); $this->assertEquals('Escape & Description2', $doc->get('description2')); $this->assertEquals('User Escape & Name', $doc->get('userfullname')); $this->assertEquals('Course & Title', $doc->get('coursefullname')); // Export for template, and see if it is escaped. $export = $doc->export_for_template($renderer); $this->assertEquals('Escape & Title', $export['title']); $this->assertEquals('Escape & Content', $export['content']); $this->assertEquals('Escape & Description1', $export['description1']); $this->assertEquals('Escape & Description2', $export['description2']); $this->assertEquals('User Escape & Name', $export['userfullname']); $this->assertEquals('Course & Title', $export['coursefullname']); }
/** * @dataProvider file_indexing_provider */ public function test_alloweduserid($fileindexing) { $this->engine->test_set_config('fileindexing', $fileindexing); $area = new core_mocksearch\search\mock_search_area(); $record = $this->generator->create_record(); // Get the doc and insert the default doc. $doc = $area->get_document($record); $this->engine->add_document($doc); $users = array(); $users[] = $this->getDataGenerator()->create_user(); $users[] = $this->getDataGenerator()->create_user(); $users[] = $this->getDataGenerator()->create_user(); // Add a record that only user 100 can see. $originalid = $doc->get('id'); // Now add a custom doc for each user. foreach ($users as $user) { $doc = $area->get_document($record); $doc->set('id', $originalid . '-' . $user->id); $doc->set('owneruserid', $user->id); $this->engine->add_document($doc); } $this->engine->area_index_complete($area->get_area_id()); $querydata = new stdClass(); $querydata->q = 'message'; $querydata->title = $doc->get('title'); // We are going to go through each user and see if they get the original and the owned doc. foreach ($users as $user) { $this->setUser($user); $results = $this->search->search($querydata); $this->assertCount(2, $results); $owned = 0; $notowned = 0; // We don't know what order we will get the results in, so we are doing this. foreach ($results as $result) { $owneruserid = $result->get('owneruserid'); if (empty($owneruserid)) { $notowned++; $this->assertEquals(0, $owneruserid); $this->assertEquals($originalid, $result->get('id')); } else { $owned++; $this->assertEquals($user->id, $owneruserid); $this->assertEquals($originalid . '-' . $user->id, $result->get('id')); } } $this->assertEquals(1, $owned); $this->assertEquals(1, $notowned); } // Now test a user with no owned results. $otheruser = $this->getDataGenerator()->create_user(); $this->setUser($otheruser); $results = $this->search->search($querydata); $this->assertCount(1, $results); $this->assertEquals(0, $results[0]->get('owneruserid')); $this->assertEquals($originalid, $results[0]->get('id')); }