2

We have a code in our solution that checks if the item is a bucket using

Sitecore.Buckets.Managers.BucketManager.IsBucket(bucketItem)

I'm creating a unit test using fakeDb and my code is this

var bucketId = Sitecore.Data.ID.NewID;

using (var db = new Db {    
     new DbItem("Bucket", bucketId)
})

var bucketItem = db.GetItem(bucketId);

bucketItem.Editing.BeginEdit();
bucketItem[Sitecore.Buckets.Util.Constants.BucketableField] = "1";
bucketItem[Sitecore.Buckets.Util.Constants.IsBucket] = "1";
bucketItem.Editing.EndEdit();

if (BucketManager.IsBucket(bucketItem))
{
     //DO STUFF HERE WHEN ITEM IS BUCKET
}

The IF statement is returning false. How do I convert the item to be a bucket in C# code?

3
  • what is the value of bucketItem[Sitecore.Buckets.Util.Constants.IsBucket] after your bucketItem.Editing.EndEdit() ?
    – Richard Seal
    May 31, 2019 at 14:48
  • Why are you unit testing the framework anyway?
    – Mark Cassidy
    Jun 1, 2019 at 15:17
  • We created a custom pipeline code to use displayname of the items under a bucket. So we need to identify if a bucket item was used based only from the url
    – qwert
    Jun 3, 2019 at 9:20

1 Answer 1

1

This code should be enough:

var bucketItem = db.GetItem(bucketId);

bucketItem.Editing.BeginEdit();
bucketItem[Sitecore.Buckets.Util.Constants.IsBucket] = "1";
bucketItem.Editing.EndEdit();

You don't need to set BucketableField to "1";

The reason why your test doesn't work is that when you call BucketManager.IsBucket it calls

BucketManager.Provider.IsBucket(item)

The problem now is that BucketManager.Provider is read from the config, which must be missing when your test is running. That's why BucketManager uses NullBucketProvider as a fallback when nothing is configured and NullBucketProvider.IsBucket(item) returns always false.

2
  • Thanks for this, but do you know how to make this return true?
    – qwert
    Jun 3, 2019 at 9:26
  • 1
    I'm afraid I can't help you with that. But you can just use if (MainUtil.GetBool(bucketItem[Sitecore.Buckets.Util.Constants.IsBucket], false)) and don't waste your time on BucketManager
    – Marek Musielak
    Jun 3, 2019 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.