1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
 * 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();
    }
}