Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  namespace core;
  18  
  19  use csv_export_writer;
  20  use csv_import_reader;
  21  
  22  defined('MOODLE_INTERNAL') || die();
  23  
  24  global $CFG;
  25  require_once($CFG->dirroot . '/lib/csvlib.class.php');
  26  
  27  /**
  28   * Tests csv import and export functions.
  29   *
  30   * @package    core
  31   * @category   test
  32   * @copyright  2012 Adrian Greeve
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class csvclass_test extends \advanced_testcase {
  36  
  37      protected $testdata = array();
  38      protected $teststring = '';
  39      protected $teststring2 = '';
  40      protected $teststring3 = '';
  41      protected $teststring4 = '';
  42  
  43      protected function setUp(): void {
  44  
  45          $this->resetAfterTest();
  46  
  47          $csvdata = array();
  48          $csvdata[0][] = 'fullname';
  49          $csvdata[0][] = 'description of things';
  50          $csvdata[0][] = 'beer';
  51          $csvdata[1][] = 'William B Stacey';
  52          $csvdata[1][] = '<p>A field that contains "double quotes"</p>';
  53          $csvdata[1][] = 'Asahi';
  54          $csvdata[2][] = 'Phillip Jenkins';
  55          $csvdata[2][] = '<p>This field has </p>
  56  <p>Multiple lines</p>
  57  <p>and also contains "double quotes"</p>';
  58          $csvdata[2][] = 'Yebisu';
  59          $this->testdata = $csvdata;
  60  
  61          // Please note that each line needs a carriage return.
  62          $this->teststring = 'fullname,"description of things",beer
  63  "William B Stacey","<p>A field that contains ""double quotes""</p>",Asahi
  64  "Phillip Jenkins","<p>This field has </p>
  65  <p>Multiple lines</p>
  66  <p>and also contains ""double quotes""</p>",Yebisu
  67  ';
  68  
  69          $this->teststring2 = 'fullname,"description of things",beer
  70  "Fred Flint","<p>Find the stone inside the box</p>",Asahi,"A fourth column"
  71  "Sarah Smith","<p>How are the people next door?</p>,Yebisu,"Forget the next"
  72  ';
  73  
  74          $this->teststring4 = 'fullname,"description of things",beer
  75  "Douglas Dirk","<p>I am fine, thankyou.</p>",Becks
  76  
  77  "Addelyn Francis","<p>Thanks for the cake</p>",Becks
  78  "Josh Frankson","<p>Everything is fine</p>",Asahi
  79  
  80  
  81  "Heath Forscyth","<p>We are going to make you lose your mind</p>",Fosters
  82  ';
  83      }
  84  
  85      public function test_csv_functions() {
  86          global $CFG;
  87          $csvexport = new csv_export_writer();
  88          $csvexport->set_filename('unittest');
  89          foreach ($this->testdata as $data) {
  90              $csvexport->add_data($data);
  91          }
  92          $csvoutput = $csvexport->print_csv_data(true);
  93          $this->assertSame($csvoutput, $this->teststring);
  94  
  95          $test_data = csv_export_writer::print_array($this->testdata, 'comma', '"', true);
  96          $this->assertSame($test_data, $this->teststring);
  97  
  98          // Testing that the content is imported correctly.
  99          $iid = csv_import_reader::get_new_iid('lib');
 100          $csvimport = new csv_import_reader($iid, 'lib');
 101          $contentcount = $csvimport->load_csv_content($this->teststring, 'utf-8', 'comma');
 102          $csvimport->init();
 103          $dataset = array();
 104          $dataset[] = $csvimport->get_columns();
 105          while ($record = $csvimport->next()) {
 106              $dataset[] = $record;
 107          }
 108          $csvimport->cleanup();
 109          $csvimport->close();
 110          $this->assertSame($dataset, $this->testdata);
 111  
 112          // Testing for the wrong count of columns.
 113          $errortext = get_string('csvweirdcolumns', 'error');
 114          $iid = csv_import_reader::get_new_iid('lib');
 115          $csvimport = new csv_import_reader($iid, 'lib');
 116          $contentcount = $csvimport->load_csv_content($this->teststring2, 'utf-8', 'comma');
 117          $importerror = $csvimport->get_error();
 118          $csvimport->cleanup();
 119          $csvimport->close();
 120          $this->assertSame($importerror, $errortext);
 121  
 122          // Testing for empty content.
 123          $errortext = get_string('csvemptyfile', 'error');
 124  
 125          $iid = csv_import_reader::get_new_iid('lib');
 126          $csvimport = new csv_import_reader($iid, 'lib');
 127          $contentcount = $csvimport->load_csv_content($this->teststring3, 'utf-8', 'comma');
 128          $importerror = $csvimport->get_error();
 129          $csvimport->cleanup();
 130          $csvimport->close();
 131          $this->assertSame($importerror, $errortext);
 132  
 133          // Testing for a tab separated file.
 134          // The tab separated file has a trailing tab and extra blank lines at the end of the file.
 135          $filename = $CFG->dirroot . '/lib/tests/fixtures/tabfile.csv';
 136          $fp = fopen($filename, 'r');
 137          $tabdata = fread($fp, filesize($filename));
 138          fclose($fp);
 139          $iid = csv_import_reader::get_new_iid('tab');
 140          $csvimport = new csv_import_reader($iid, 'tab');
 141          $contentcount = $csvimport->load_csv_content($tabdata, 'utf-8', 'tab');
 142          // This should import four rows including the headings.
 143          $this->assertEquals($contentcount, 4);
 144  
 145          // Testing for empty lines.
 146          $iid = csv_import_reader::get_new_iid('blanklines');
 147          $csvimport = new csv_import_reader($iid, 'blanklines');
 148          $contentcount = $csvimport->load_csv_content($this->teststring4, 'utf-8', 'comma');
 149          // Five lines including the headings should be imported.
 150          $this->assertEquals($contentcount, 5);
 151      }
 152  }