/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class PersonalEvaluationClipEditControllerTest {

    static testMethod void myUnitTest() {
        SMARM__c s = new SMARM__c(
            OwnerId = '00510000000fSYI',
            Status__c = 'Delete',
            Review_Practitioner__c = UserInfo.getUserId(),
            Review_Target__c = UserInfo.getUserId(),
            
            Image1__c = 'img1',
            ReportName1__c = 'report1',
            Comment1__c = 'comment1',
            
            Image3__c = 'img3',
            ReportName3__c = 'report3',
            Comment3__c = 'comment3',
            
            Image4__c = 'img4',
            ReportName4__c = 'report4',
            Comment4__c = 'comment4'
        );
        insert s;
        
        // Attachment att1 = new Attachment(
        //     Name = '1.report1',
        //     ParentId = s.Id,
        //     Body = EncodingUtil.base64Decode('img1')
        // );
        // Attachment att2 = new Attachment(
        //     Name = '3.report3',
        //     ParentId = s.Id,
        //     Body = EncodingUtil.base64Decode('img3')
        // );
        // Attachment att3 = new Attachment(
        //     Name = '4.report4',
        //     ParentId = s.Id,
        //     Body = EncodingUtil.base64Decode('img4')
        // );
        // insert new Attachment[] {att1, att2, att3};

        List<ContentVersion> versionList = new List<ContentVersion>();

        ContentVersion version = new ContentVersion();
        version.VersionData = EncodingUtil.base64Decode('img1');
        version.Title = 'test';
        version.PathOnClient = 'test';
        version.ContentLocation = 's';
        versionList.add(version);
        insert versionList;

        List<Id> idList = new List<Id>();
        for (ContentVersion newVersion : versionList) {
            idList.add(newVersion.Id);
        }
        versionList = [select ContentDocumentId,Title from ContentVersion where Id in: idList];

        ContentDocumentLink cDocLink = new ContentDocumentLink();
        cDocLink.ContentDocumentId = versionList[0].ContentDocumentId;         
        cDocLink.LinkedEntityId = s.id;    
        cDocLink.ShareType = 'V';                                   
        cDocLink.Visibility = 'AllUsers';
        
        PageReference page = new PageReference('/apex/PersonalEvaluationClipEdit?id=' + s.Id);
        System.Test.setCurrentPage(page);
        PersonalEvaluationClipEditController pece = new PersonalEvaluationClipEditController();
        pece.init();
        
        System.assertEquals(3, pece.clipList.size());
        System.assertEquals('1,2,3', pece.newOrder);
        
        pece.newOrder = '2,3,1';
        pece.saveOrder();
        s = [select Id,ReportName1__c, ReportName2__c, ReportName3__c, ReportName4__c from SMARM__c where Id = :s.Id];
        System.assertEquals('report3', s.ReportName1__c);
        System.assertEquals('report4', s.ReportName2__c);
        System.assertEquals('report1', s.ReportName3__c);
        System.assertEquals(null, s.ReportName4__c);

        // List<Id> idList = new List<Id>();
        // for (ContentVersion newVersion : versionList) {
        //     idList.add(newVersion.Id);
        // }
        // versionList = [select ContentDocumentId,Title from ContentVersion where Id in: idList];

        // List<ContentDocumentLink> linkList = new List<ContentDocumentLink>();
        // for (ContentVersion cVersion : versionList) {
        //     ContentDocumentLink cDocLink = new ContentDocumentLink();
        //     cDocLink.ContentDocumentId = cVersion.ContentDocumentId;         
        //     cDocLink.LinkedEntityId = s.Id;    
        //     cDocLink.ShareType = 'V';                                   
        //     cDocLink.Visibility = 'AllUsers';
        //     linkList.add(cDocLink);
        // }
        
        // insert linkList;
        
        Map<String, Object> rtn = PersonalEvaluationClipEditController.getAttachmentBody(s.Id);
        // System.assertEquals(3, rtn.get('AttCnt'));
        
        pece.init();
        // pece.delLineNo = 2;
        pece.deleteClip();
        //s = [select ReportName1__c, ReportName2__c, ReportName3__c, ReportName4__c from SMARM__c where Id = :s.Id];
        //System.assertEquals('report3', s.ReportName1__c);
        //System.assertEquals('report1', s.ReportName2__c);
        //System.assertEquals(null, s.ReportName3__c);
        //System.assertEquals(null, s.ReportName4__c);
        
        pece.complete();
        s = [select OwnerId, Status__c, Review_Target__c from SMARM__c where Id = :s.Id];
        // System.assertEquals(s.Review_Target__c, s.OwnerId);
        // System.assertEquals('草案中', s.Status__c);
    }

    static testMethod void myUnitTest2() {
        SMARM__c s = new SMARM__c(
            OwnerId = '00510000000fSYI',
            Status__c = 'Delete',
            Review_Practitioner__c = UserInfo.getUserId(),
            Review_Target__c = UserInfo.getUserId(),
            
            Image1__c = 'img1',
            ReportName1__c = 'report1',
            Comment1__c = 'comment1',
            
            Image3__c = 'img3',
            ReportName3__c = 'report3',
            Comment3__c = 'comment3',
            
            Image4__c = 'img4',
            ReportName4__c = 'report4',
            Comment4__c = 'comment4'
        );
        insert s;
        

        List<ContentVersion> versionList = new List<ContentVersion>();

        ContentVersion version = new ContentVersion();
        version.VersionData = EncodingUtil.base64Decode('img1');
        version.Title = 'test';
        version.PathOnClient = 'test';
        version.ContentLocation = 's';
        versionList.add(version);
        insert versionList;
        
        PageReference page = new PageReference('/apex/PersonalEvaluationClipEdit?id=' + s.Id);
        System.Test.setCurrentPage(page);
        PersonalEvaluationClipEditController pece = new PersonalEvaluationClipEditController();
        pece.init();
        
        System.assertEquals(3, pece.clipList.size());
        System.assertEquals('1,2,3', pece.newOrder);
        
        pece.newOrder = '2,3,1';
        pece.saveOrder();
        s = [select Id,ReportName1__c, ReportName2__c, ReportName3__c, ReportName4__c from SMARM__c where Id = :s.Id];
        
        Map<String, Object> rtn = PersonalEvaluationClipEditController.getAttachmentBody(s.Id);
        
        pece.init();
        pece.delLineNo = 2;
        pece.deleteClip();
        
        pece.complete();
    }
}