@isTest (seeAllData=true) // Needed because otherwise Report table is blank
private class ReportExportControllerTest {
    static ReportExportController ctrl;
    static String o;

    @isTest
    static void testErrorFlow() {
        ctrl = new ReportExportController();
        try{
            o = ctrl.getOutput();
            System.assert(false, 'Exception was expected to be thrown');
        } catch (ReportExportController.ReportExportException e){
            System.assertEquals('Missing argument: reportId', e.getMessage());
        }

        ctrl.reportId = UserInfo.getUserId(); // valid Id but not a valid report Id
        try{
            o = ctrl.getOutput();
            System.assert(false, 'Exception was expected to be thrown');
        } catch (ReportExportController.ReportExportException e){
            System.assert(e.getMessage().startsWith('Suspicious reportId:'));
        }
    }

    @isTest
    static void testPrettyCsvExport(){
        ReportExportController.mockOutput = '"Account ID","Account Name","Full Name","Account Record Type","Created Date"'
            + '"001M000000KbxBu","Abc","","Customer","01/11/2012"\n'
            + '"001M000000J9aYP","Cde","","Supplier","31/08/2012"\n'
            + '\n'
            + '\n'
            + '"Accounts without Contacts\n"'
            + '"Copyright (c) 2000-2012 salesforce.com, inc. All rights reserved.""\n'
            + '"Confidential Information - Do Not Distribute"\n'
            + '"Generated By:  X Y  03/12/2012 13:24"\n'
            + '"' + UserInfo.getOrganizationName() + '"\n';

        List<Report> reports = [SELECT Id, Name FROM Report ORDER BY Name LIMIT 1]; // Even a fresh uncustomized org should have some sample reports but it doesn't hurt to play safe.
        System.debug(reports);

        if(!reports.isEmpty()){
            ctrl = new ReportExportController();
            ctrl.reportId = reports[0].Id;
            ctrl.format = 'csv';
            ctrl.prettify = true;
            ctrl.encoding = ctrl.showDetails = 'Some random gibberish';

            System.assert(ctrl.getRequestUrl().endsWith('/' + reports[0].Id + '?export=1&xf=csv&enc=UTF-8'));

            String output = ctrl.getOutput();
            System.assert(!output.contains('"Confidential Information - Do Not Distribute"'));
        }
    }

    @isTest
    static void testPrettyPrintableViewExport(){
        ReportExportController.mockOutput = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">\n'
            + '<html>\n' // skipped a bunch of lines
            + '<!-- Start report output -->\n'
            + '<div id="fchArea"><table class="reportTable tabularReportTable" border="0" cellspacing="0" cellpadding="0"><tr id=\'headerRow_0\' bgcolor=\'#aaaaff\' class=\'headerRow\'><TH align="left" scope="col">Account ID</TH><TH align="left" scope="col">Account Name</TH><TH align="left" scope="col">Full Name</TH><TH align="left" scope="col">Account Record Type</TH><TH align="right" scope="col">Created Date</TH></tr>\n'
            + '<tr class="odd" valign="top"><td >001M000000KbxBu</td><td >Abc</td><td >-</td><td >Customer</td><td align="right">01/11/2012</td></tr>\n'
            + '<tr class="even" valign="top"><td >001M000000J9aY3</td><td >Def</td><td >-</td><td >Supplier</td><td align="right">31/08/2012</td></tr>\n'
            + '<tr bgcolor=\'#aaaacc\' class=\'grandTotal grandTotalTop\'><td colspan="7"><strong>Grand Totals (25 records)</strong></td></tr>\n'
            + '<tr bgcolor=\'#aaaacc\' class=\'grandTotal\' valign="top"><td class="nowrapCell" align="right">&nbsp;</td>\n'
            + '<td class="nowrapCell" align="right">&nbsp;</td>\n'
            + '</tr>\n'
            + '</table>\n'
            + '</div><table ><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>\n'
            + '<tr><td colspan="5"><span  class="confidential">Confidential Information - Do Not Distribute</span></td></tr>\n'
            + '<tr><td colspan="5">Copyright (c) 2000-2012 salesforce.com, inc. All rights reserved.</td></tr>\n'
            + '</table></div></div></div></div><div class="pbFooter secondaryPalette"><div class="bg"></div></div></div></div></body>\n'
            + '</html>\n';

        List<Report> reports = [SELECT Id, Name FROM Report ORDER BY Name LIMIT 1];
        System.debug(reports);

        if(!reports.isEmpty()){
            ctrl = new ReportExportController();
            ctrl.reportId = reports[0].Id;
            ctrl.format = 'printable';
            ctrl.showDetails = 'yes';

            System.assert(ctrl.getRequestUrl().endsWith('/' + reports[0].Id + '?excel=1&details=yes'));

            String output = ctrl.getOutput();
            System.assert(output.startsWith('<div id="fchArea">'));
            System.assert(!output.contains('"Confidential Information - Do Not Distribute"'));
        }
    }
}