fake-xrm-easy icon indicating copy to clipboard operation
fake-xrm-easy copied to clipboard

How to assert the entity record created in run time?

Open charliechen179 opened this issue 4 years ago • 0 comments

I have a custom workflow activity to share the target record with the specified team as follow:

`public class ShareRecordToAccessTeam : CodeActivity {

    #region "Parameter Definition"

    //[RequiredArgument]
    //[Input("Sharing Record URL")]
    //[ReferenceTarget("")]
    //public InArgument<String> SharingRecordURL { get; set; }

    [RequiredArgument]
    [Input("Team")]
    [ReferenceTarget("team")]
    public InArgument<EntityReference> Team { get; set; }

    /// <summary>
    /// Share Read privilege.
    /// </summary>
    [Input("Read Permission")]
    [Default("True")]
    public InArgument<bool> ShareRead { get; set; }

    /// <summary>
    /// Share Write privilege.
    /// </summary>
    [Input("Write Permission")]
    [Default("False")]
    public InArgument<bool> ShareWrite { get; set; }

    /// <summary>
    /// Share Delete privilege.
    /// </summary>
    [Input("Delete Permission")]
    [Default("False")]
    public InArgument<bool> ShareDelete { get; set; }

    /// <summary>
    /// Share Append privilege.
    /// </summary>
    [Input("Append Permission")]
    [Default("False")]
    public InArgument<bool> ShareAppend { get; set; }

    /// <summary>
    /// Share AppendTo privilege.
    /// </summary>
    [Input("Append To Permission")]
    [Default("False")]
    public InArgument<bool> ShareAppendTo { get; set; }

    /// <summary>
    /// Share Assign privilege.
    /// </summary>
    [Input("Assign Permission")]
    [Default("False")]
    public InArgument<bool> ShareAssign { get; set; }

    /// <summary>
    /// Share Share privilege.
    /// </summary>
    [Input("Share Permission")]
    [Default("False")]
    public InArgument<bool> ShareShare { get; set; }


    List<EntityReference> principals = new List<EntityReference>();
    #endregion
    protected override void Execute(CodeActivityContext executionContext)
    {
        #region "Load CRM Service from context"

        try
        {
        WorkFlowActivityBase objCommon = new WorkFlowActivityBase(executionContext);
        objCommon.tracingService.Trace("Load CRM Service from context --- OK");

        var caseId = objCommon.context.PrimaryEntityId;

        var entityLogicalName = objCommon.context.PrimaryEntityName;

        objCommon.tracingService.Trace("ObjectTypeCode=" + entityLogicalName + "--ParentId=" + caseId.ToString());

        #endregion

        #region "Read Parameters"
       // String _SharingRecordURL = this.SharingRecordURL.Get(executionContext);
        //if (_SharingRecordURL == null || _SharingRecordURL == "")
        //{
        //    return;
        //}
        //string[] urlParts = _SharingRecordURL.Split("?".ToArray());
        //string[] urlParams = urlParts[1].Split("&".ToCharArray());
        //string objectTypeCode = urlParams[0].Replace("etc=", "");
        //string objectId = urlParams[1].Replace("id=", "");
        //objCommon.tracingService.Trace("ObjectTypeCode=" + objectTypeCode + "--ParentId=" + objectId);

        EntityReference teamReference = this.Team.Get(executionContext);
        principals.Clear();

        if (teamReference != null) principals.Add(teamReference);

        #endregion

        #region "ApplyRoutingRuteamReferenceleRequest Execution"
        //string EntityName = objCommon.sGetEntityNameFromCode(objectTypeCode, objCommon.service);

        //EntityReference refObject = new EntityReference(EntityName, new Guid(objectId));
        EntityReference refObject = new EntityReference(entityLogicalName, caseId);
        objCommon.tracingService.Trace("Grant Request--- Start");
        GrantAccessRequest grantRequest = new GrantAccessRequest();
        grantRequest.Target = refObject;
        grantRequest.PrincipalAccess = new PrincipalAccess();
        grantRequest.PrincipalAccess.AccessMask = (AccessRights)getMask(executionContext);
        foreach (EntityReference principalObject2 in principals)
        {
            grantRequest.PrincipalAccess.Principal = principalObject2;
            GrantAccessResponse grantResponse = (GrantAccessResponse)objCommon.service.Execute(grantRequest);
        }

        objCommon.tracingService.Trace("Grant Request--- end");

        #endregion

        }
                    }

    }

    UInt32 getMask(CodeActivityContext executionContext)
    {
        bool ShareAppend = this.ShareAppend.Get(executionContext);
        bool ShareAppendTo = this.ShareAppendTo.Get(executionContext);
        bool ShareAssign = this.ShareAssign.Get(executionContext);
        bool ShareDelete = this.ShareDelete.Get(executionContext);
        bool ShareRead = this.ShareRead.Get(executionContext);
        bool ShareShare = this.ShareShare.Get(executionContext);
        bool ShareWrite = this.ShareWrite.Get(executionContext);

        UInt32 mask = 0;
        if (ShareAppend)
        {
            mask |= (UInt32)AccessRights.AppendAccess;
        }
        if (ShareAppendTo)
        {
            mask |= (UInt32)AccessRights.AppendToAccess;
        }
        if (ShareAssign)
        {
            mask |= (UInt32)AccessRights.AssignAccess;
        }

        if (ShareDelete)
        {
            mask |= (UInt32)AccessRights.DeleteAccess;
        }
        if (ShareRead)
        {
            mask |= (UInt32)AccessRights.ReadAccess;
        }
        if (ShareShare)
        {
            mask |= (UInt32)AccessRights.ShareAccess;
        }
        if (ShareWrite)
        {
            mask |= (UInt32)AccessRights.WriteAccess;
        }

` Now I want to unit test it with FakeXrmEasy like blow:

` public void When_case_saved_with_valid_transfer_to() { XrmFakedContext fakedContext = new XrmFakedContext();

        var wfContext = fakedContext.GetDefaultWorkflowContext();
        
        var service = fakedContext.GetFakedOrganizationService();
        
        var case1 = new Incident()
        {
            Id = Guid.NewGuid(),
            Title="test case"
        };


        var team = new Team()
        {
            Id = Guid.NewGuid(),
            Name = "CaseAssignedToCSLP",
            TeamType = team_type.Access
        };

       var pOA = new PrincipalObjectAccess();

        fakedContext.Initialize(new List<Entity>() {
            team,
            case1,
            pOA

        });


        var inputs = new Dictionary<string, Object>
        {
            {"Team", team.ToEntityReference()}//,
            //{"Read Permission", "True"},
            //{"Write Permission", "False"},
            //{"Delete Permission", "False"},
            //{"Append Permission", "False"},
            //{"Append To Permission", "False"},
            //{"Share Permission", "False"}

        };

        ParameterCollection inputParameter = new ParameterCollection();
        inputParameter.Add("Target", case1);
        wfContext.MessageName = "Update";
        wfContext.PrimaryEntityId = case1.Id;
        wfContext.PrimaryEntityName = Incident.EntityLogicalName;
        wfContext.InputParameters = inputParameter;

        var result = fakedContext.ExecuteCodeActivity<ShareRecordToAccessTeam>(wfContext,inputs,null);

       var actual = fakedContext.CreateQuery<PrincipalObjectAccess>()
            .Where(p => (p.PrincipalId == team.Id) && (p.ObjectId == case1.Id))
            .Select(p => new { p.AccessRightsMask, p.Attributes,p.EntityState})
            .FirstOrDefault();
            

        Assert.True(r.......);

        //
        //Assert.True();
    }

        return mask;

    }
}

` The problem is the POA instance won't be created in context but created in the execution of workflow activity. How can I get access to this entity record? Thanks!

``

charliechen179 avatar Aug 08 '19 13:08 charliechen179