Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

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